Java's Creators Rejected Multiple Inheritance - Here's Why

preview_player
Показать описание
Java's creators didn't include multiple inheritance. Have you ever wondered why?

Multiple inheritance was excluded from Java from the very beginning. But why was multiple inheritance not included in Java when so many other languages include it?

Learn all about Multiple Inheritance, including why Java doesn't have it, in this Java video tutorial.

Learn or improve your Java by watching it being coded live!

Hi, I'm John! I'm a Lead Java Software Engineer and I've been in the programming industry for more than a decade. I love sharing what I've learned over the years in a way that's understandable for all levels of Java learners.

Let me know what else you'd like to see!

Links to any stuff in this description are affiliate links, so if you buy a product through those links I may earn a small commission.

00:00 Why No Multiple Inheritance?
00:28 Inheritance in Java
02:40 Multiple Inheritance
04:05 The Diamond Problem
06:26 What Java Does About it and Why
08:14 Get What You Need Without Multiple Inheritance

📕 THE best book to learn Java, Effective Java by Joshua Bloch

📕 One of my favorite programming books, Clean Code by Robert Martin

🎧 Or get the audio version of Clean Code for FREE here with an Audible free trial

🖥️Standing desk brand I use for recording (get a code for $30 off through this link!)

📹Camera I use for recording:

🎙️Microphone I use (classy, I know):

Donate with PayPal (Thank you so much!)

☕Complete Java course:

Рекомендации по теме
Комментарии
Автор

You explain these concepts really well. Even after 5 years of coding Java, I still learn from you

mariovelez
Автор

I really appreciate the way you take high level concepts and present them in a digestible manner. I would love to hear you talk about design patters in Java. I feel it's a topic that is somewhat ambiguous and I feel that your high level analysis of it would greatly help me and many others!

Riley-mlvo
Автор

Spicy comment incoming:

So, I can tell that this is a beginner friendly channel, but it would've been nice to at least mention in passing that inheritance is a bad practice ("Composition over Inheritance"). Here's a few resources that have great coverage of this concept:

- I greatly appreciate that you've included "Effective Java" in the video description, which is a beautifully written book -- it actually covers this concept as Item 18, which is titled... "Composition over Inheritance".
- I'd imagine that you're also familiar with the "Design Patterns" book by "the gang of four" (another very classic book in the industry) which also covers the concept of "Composition over Inheritance" in the very first chapter (p. 18 of my copy of the book).
- Some languages such as Golang (the language that Google created for their own software engineers) go as far as to not even implement inheritance within the language.


SDFC
Автор

This problem somewhat returned the moment when interfaces got default implementations in Java 8. So in some cases a developer is forced to choose which interface default implementation he wants to use, but it's compiler enforced and quite rare in practice. As for a diamond issue the bigger problem in my opinion are fields rather than methods. In case of methods you can choose an implementation as mentioned before, but in case of fields you can't just "pick" which field version you want to use safely without potentially messing up something.

pawewodarczyk
Автор

I discovered your channel today, let me tell you, your content is incredible! I love how simplified yet informative your explanations are, it says alot about how well you know what you're doing, I love the jokes you implement here and there as well, the whole thing is very interesting and educational from start to finish, light hearted and well put together. perfect to my taste! 🎉

sabeib
Автор

John, you the definition of what makes a good teacher. You have passion, knowledge, and dedication. Teaching isn't a skill. It's simply someone who understands something so well that they can break it down to it's simplest form and explain it in a way without trying to show off. You just that, every teacher should learn from you!

JamieCrew
Автор

8:06 I've had a couple instances where I've wished Java had multiple inheritance. It wasn't really the only solution, just the alternative was a messy approach that involved duplicating a lot of code.

Sollace
Автор

maybe in this topic could have been mentioned implementation of method from two different Interfaces with same method signature

jozef_franek
Автор

I was wanting for multiple inheritance recently. I had a PythonScript class that acted as a large superclass for anything that was a script (entity script, item script). I also had a class called UserInterface which was a large superclass for classes that are UI (editorUI elements, Main menu UIs). I needed a UIScript class which would extend both and have the needed protected fields from both but it wasn't possible under the current system. UIScript extends PythonScript and within the script file, an object called ScriptedUI is constructed and used as the actual UI. Other than this recent problem, I would never have needed MI.

ChrisBrown-dntf
Автор

I have actually tried to implement annotation interfaces in a class before and I was surprised when I saw you do it too.
And yes I made a class that implements Deprecated, and realized that the required methods are actually the annotation members, as well as the annotationType() method. And while testing this, I found out that annotation interfaces without any members are actually functional interfaces, and giving them the @FunctionalInterface annotation works.

ManosSef
Автор

8:00 Well here's the sitch:
Cat is an abstract class written by someone else that you aren't allowed to change.
Dog is from a licensed library package which you don't have the source code for.
Your manager is telling you to implement Cog in 5 minutes since "everything's been done already".

ShadoFXPerino
Автор

I find the diamond problem argument a bit of a meme because of the existence of default interface methods since Java 8. By that I mean it already exists in the language and you have tools to resolve that conflict while preserving the multi inheritance of interfaces.
The more likely reason I can think of is performance - with single inheritance, class fields always have the same offset within the memory of an object and type casting becomes basically free.

Quinteger
Автор

The explanation is very clear and concise. James Gosling would be envious.

zhalagurbanzade
Автор

If the diamond problem is the reason for not having multiple inheritance, then why can you implement multiple interfaces that could have the same default method implementation? In my opinion,

It would be neater if the subclass would treat a superclass method implementation conflict as an abstract method, so that you'd have to be explicit about which implementation you'd want (or if you wanted a whole different implementation). Only if the method was final it should give an exception.

rikschaaf
Автор

There one case where the multiple inheritance is missing, it's when you want to inherit from a class that it's not yours and don't duplicate code.

For exemple, if you have an interface that allow to get/set data from a database with a standardized solution (use of getType() to know table, then fetch provided attribut) and give those function to 2 objects, then you can either duplicate the whole code, or create a common abstract object.
But what if you want to add this feature to object that can be create in database, like MyCat & MyDog, but you can't on the Animal, Cat & Dog because it's not your ?

Before Java 1.7, it was not possible without code duplication.
Since Java 1.8, you may try to provide default function on interface, but you still need a getType() which is not available in your interface.
You can trick java by creating this method in the interface, because if the method exist in at least one of your parent, compilation work.

The downside is that you need to declare an AnimaleLike interface to provide signature of all needed function to your own interface, so you still have a bit of a duplication.

joeysarie
Автор

To me multiple inheritance is just another useful tool in the box that unfortunately is not available in Java. One appropriate use would be the implementation of modular classes that can then be combined into a whole range of different combinations. Coming from C++ I sometimes do miss that language feature.
Of course one can emulate that behavior with Java constructs like helper classes and/or default implementations in interfaces (which again introduces the oh-so-terrible diamond problem that doesn't actually exist in Java.).
At the end these workaraounds require me to spread code all over the place that IMO better should be kept together in a single class.
Of course one can do all kinds of stupid things using multiple inheritance. As with almost any other advanced tool.
Carpenters use nail guns. When used appropriately they increase productivity and allow to work faster and with more accuracy. Sure you can shoot yourself in the foot with a nail gun. That is why you never let the inexperienced apprentice use it unsupervised, you observe best practises and safety procedures and you always wear protective gear.
Only the Java language designers would come to the conclusion that nail guns should be banned altogether.

andreassumerauer
Автор

I've got one!! Please believe me :P I'm working on a graphing package in Java, and one of the superclasses (abstract) is Plot, from which inherit LinePlot and ScatterPlot (not abstract). I have been wanting to create a LineScatterPlot class, and have kept on thinking that this would have been a perfect time for multiple inheritance. But yes, I will find a way around it... :P

Walkingdeadman
Автор

Great video as usual! You do a fantastic work, it’s appreciated, we need more people like you 🎉

aminembh
Автор

I Really like the way you explain coding stuff, even things I already know I watch the video patiently and learn something new. The way you speak english is nice even for foreign like me. Thanks again John ! See you next time!

lucashenrique
Автор

John, just a great video, i would appreciate if you can have some more advanced course on Java such as concurrency, i would definitely pay to watch.

hz