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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you've probably heard that Java doesn't support multiple inheritance that was a very deliberate choice that Java's creators made but why would they choose not to include a feature like multiple inheritance when so many other popular languages like Python and C plus have it also I think they were correct to leave it out and I'll tell you why before we get right into it I also have a full Java course available in a link down in the description there you'll find dozens of videos exclusive to the course where you'll find tons of java knowledge so go check it out now before we talk about why Java's creators chose not to include multiple inheritance we first have to understand exactly what it is now as you may already know inheritance and object-oriented programming is when one class acquires the fields and methods of another so as a very simple example let's say I had this class called animal now this class has a couple of different attributes that would make sense for any animal like say age and name and maybe some kind of a method that enables that animal to make noise now then of course there are a ton of different types of animals that might have their own unique sorts of attributes so I might have a cat class that inherits from the animal class now in this relationship the animal class is called the parent class or sometimes the superclass and the cat class is the child class or subclass by extending the animal class the cat class automatically gets the fields and the methods from the animal class a cat automatically can have an age and a name and some way to make noise but you can also customize that method of making noise to make it specific to cats so here's what that inheritance relationship looks like in the Java code so this class animal is going to be the parent class to our cat class so now in Java if we want this cat class to be a child of the animal class all we have to do is right here at the end of the class declaration is ADD extends and animal so now any cat that we make will also have all of the attributes and methods of the animal class so for example back over here in our main method we can create a new cat we'll call it my cat equals new cat and even though we don't have the make noise method defined specifically in the cat class we can call my cat dot make noise and each cat also has access to the fields that are defined in the animal class so for example I can set a name on my cat even though the name field is only defined in animals my cat dot name equals Kramer so this is how inheritance Works in Java but what exactly is multiple inheritance multiple inheritance is when one class has more than one parent class so let's check out what that would look like so here we have our cat class that extends from the animal class let's say that on top of that we had another other class that extended from animal called dogged and remember there's nothing at all wrong with that this is not what multiple inheritance means a class can totally have more than one child class but what if on top of that we wanted to create another class called Cog that inherited all the features of both a cat and a dog in concept that seems like it might be okay right but let's see what happens when we try to implement that in Java code so let's hop over to our Cog class notice right now it's an empty class it's not extending anything so first let's have it extend the cat class okay that all seems to work fine no errors but when we try and add the dog class we now get an error and it says class cannot extend multiple classes so this is Java telling you flat out you can't do multiple inheritance you're not allowed to inherit from or extend more than one class so clearly Java doesn't allow multiple inheritance but now the question is why what's the harm what's the problem a lot of people when they talk about potential problems or issues with multiple inheritance they'll talk about something called The Diamond problem some people even call it the deadly Diamond of death sounds pretty scary right well here's what it is so let's say that we had our classes set up just like we do here and then let's say that in our cat class we implemented that make noise method in a certain way that would make sense for a cat so in our cat class it might make sense to print out meow terrible handwriting but you get the picture and then what if similarly over in our dog class we implemented that make noise method in a way that made sense for a dog so for a dog maybe we would print out woof but now we have this new Cog class which we're trying to have inherit from both the cat and the dog class and of course since it inherits all the methods from both of its parents it's going to inherit the make noise method from each of those parents but the problem is it's completely ambiguous which version of the make noise method that this Cog class should inherit should it inherit the functionality from the cat class that prints out meow or should it inherit the functionality from dog that prints Out woof this problem is often called The Diamond problem perhaps for obvious reasons the pattern of inheritance that causes this kind of issue is shaped like a diamond so what does Java do about this well basically it just doesn't allow it Java doesn't allow any multiple inheritance so you will never run into the diamond problem but like I said in the intro there are other programming languages like Python and C plus that do allow multiple inheritance so what do those languages do when the diamond problem comes up for the most part they just kind of pick one and as the programmer you just have to know how that particular programming language chooses which one that it accepts for example let's say that this was a programming language that did support multiple inheritance if you are allowed to extend both cat and dog and you were inheriting a method the same method from both of them it has to choose which version of that method that it should inherit and usually it would just choose the first one in the list that you're inheriting from that can change a bit depending on the language that you're using especially for really deep complicated inheritance hierarchies it gets really messy of course the people who created Java could have done exactly the same thing they could have allowed multiple inheritance and then whenever this diamond problem came up it could just pick one and call it a day but instead they chose not to so why number one they wanted to keep the language as simple as they could if they allowed multiple inheritance developers that didn't have a really good feel for how it worked especially when the diamond problem came up would run into all kinds of strange functionality and unexpected problems and number two in languages where multiple inheritance is allowed it's used incredibly rarely basically in a real world scenario there's no reason that you ever have to use it to do what you need to do when they were creating the Java programming language they modeled a lot of it after C plus which was a programming language that a lot of developers use at the time and they did that to make it familiar with those developers which makes sense one of Java's original creators a guy named James Gosling actually wrote about it at the time he said Java omits many rarely used poorly understood confusing features of C plus than in our experience bring more grief than benefit and one of those was multiple inheritance now honestly this argument really resonates with me I've been programming professionally in Java for more than 10 years and I can't think of even one time when I was like oh man I have this big problem and the only way I could possibly solve it would be with multiple inheritance but I don't have it in Java curse you James Gosling now I'm sure sure some of you are just screaming at your screen right now with the perfect example of when multiple inheritance was the only decent solution to some problem you were having if that's you number one let me know in the comments and number two I don't believe you so yes it's true Java does not support multiple inheritance however Java does support a class implementing multiple interfaces that's different from multiple inheritance but almost always gives you the functionality that you're actually looking for and implementing multiple interfaces is something we do all the time all over our code AS Enterprise level Java developers for example in our situation with the Cog class we couldn't extend both cat and dog that's not allowed but maybe what we needed isn't really a class that is both a cat and a dog because that concept doesn't really make much sense anyway maybe what we wanted was more of a cat that offers some of the functionality that a dog might have like Forex example the ability to play fetch well when we want to specify something that a class is able to do like play fetch that's exactly what interfaces are for now interfaces are a huge separate topic that warrant an entire video of their own but briefly an interface is basically just a contract so it says any class that implements me must be able to do these things then when a class implements that interface they are required to provide that functionality so for example I have this interface called Fetch and inside this interface I can add the signature of a fetch method and it takes in no parameters and has no return type so all this interface does is say that any class that chooses to implement this interface must provide their own implementation of this fetch method so back over in our Cog class instead of extending both cat and dog I can extend only the a cat class and then implement the fetch interface and now you can see that we're getting an error here and it's saying that this Cog class because it is implementing the fetch interface it is required to implement this fetch method we signed up for the contract that said that this class will be able to fetch but right now we aren't fulfilling the contract so we have to add that functionality we have to add that method so let's go ahead and implement it and if you're using IntelliJ you can just hover over this error and click Implement methods and then hit OK and it will provide you the method signature for the method that you have to implement okay so we are a cog which is a cat that can fetch so how would a cat fetch so how about we would print out I got the stick but not because you asked me to I did it because I wanted to it will fetch if it pleases it to fetch so now though if I go all the way back to my main method if I create a new Cog object let's call it my Cog equals new Cog since Cog implements the fetch interface I know that I can call mycog Dot Fetch and now if we go ahead and run it of course our fetching cat works as planned what's cool about implementing interfaces is that you don't have the same limitation that you have for extending classes you are totally allowed to implement as many interfaces as you want in a class so back over in our Cog class in addition to implementing fetch we can add any number of other interfaces so we can Implement runnable comparable deprecated serializable so you get the point right you can Implement as many interfaces as you want now of course for all of these interfaces that you implement you are required to then Implement all the methods that they require you to implement so if I hover over this and said okay go ahead and Implement all of these methods I have a lot of work to do in order to fulfill the contracts that I've signed up for for all the interfaces that I'm implementing now the reason that Java gives you the ability to implement multiple interfaces but not inherit from multiple classes is that an interface specifies what you are able to do not what you are and it makes plenty of sense to allow a class to be able to do all kinds of different things extending classes however really specifies what a class is and the creators of java decided that in practice it really made more sense to have a class only be able to extend one other class rather than introduce what they consider to be a ton of unnecessary complexity into the class hierarchies it's a lot more likely that you'll need a cat that can fetch than it is that you'll need some sort of mutant cat dog hybrid if you enjoyed this video or learned something please let me know by hitting the like button and be sure to subscribe so you don't miss each new video and if you didn't like this video thanks for watching anyway not really sure why you're still here but I appreciate it thanks so much for watching I really do appreciate all your support and comments they really mean the world to me and I'll see you next time
Info
Channel: Coding with John
Views: 67,675
Rating: undefined out of 5
Keywords: java, codingwithjohn, coding with john, java beginner lesson, multiple inheritance in java, multiple inheritance, diamond problem in java, inheritance in java, java diamond problem, java multiple inheritance, multiple inheritance in java example, does java support multiple inheritance, why java doesnt support multiple inheritance, java (programming language), java tutorial, java full course
Id: 1-JBFJ8Xar0
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Mon Oct 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.