Multiple Inheritance in Java (Intro to Interfaces)
Articles Blog

Multiple Inheritance in Java (Intro to Interfaces)

Hey guys, this is developer bhanu and today we’re going to discuss about multiple inheritance in Java so why is there a question mark after java? Let’s see that as we go forward! Alright, so.. What basically is multiple inheritance? so we can say that multiple inheritance is nothing but reversed hierarchical inheritance so if you have observed in my video about hierarchical inheritance, you have seen That, uhhmm, there is one parent class and several child classes. or one base class and several sub classes so, in this case, we will have there are more than one parent class and one child class in this case, one class will inherit objects from different parent classes. in case of multiple inheritance. so uhh.. to see a blue print of it or you know? just a block?! of it we can look at it like this There is parent one and parent two and the Child is getting access to the objects of parent one and parent two This is basically a multiple inheritance. but this is not supported in Java yes, this is not supported in java this is because there may be situations where parent one and parent two both of them have the similar or the same method or methods with the same name so in this case, we have method example. with the same name in both parent one and parent two so what if the child inherits parent one and parent two and calls the example method now what class or what method will be executed or what method will be called to the child class? so to avoid this completely There is a… they (java) removed multiple inheritance. but, we can achieve that alternatively like, you know, using some other… indirectly (uffff) if.. if you… if you know what I’m saying. so we can achieve multiple inheritance indirectly but that will be… I think in another video or if possible I’ll make it in this video only! But let’s just focus on multiple inheritance with classes directly (for now) so if we go to the coding so let’s just go to the code part and see what’s gonna happen in multiple inheritance so I’m creating a file The name… A’ite yup. so actually I have changed from notepad++ to sublime text because it is a little more fast and advanced Alright, so.. uuhmmm… yeah. let’s directly open it. WHAT AM I THINKING!!?? A’ite as you all know I will start this with a package and this will be examples. so if we say, “Class” Let’s call it parent one and parent one has a method that’s called parentMethod.. alright, nice! let’s say some… mhm.. ok this is a method from parent one. alright! so the same way, let’s make another Parent Two so let me change this to.. Two and the parent method will be the same because I have told you in the presentation that if both the classes have the same method or the method with same name what would happen? so let us say system.out.println This is a method from parent two so we have a child class and this child class will extend Parent one and Parent two Alright so, uh we can directly, uh… you know? have the main method here but but it will not be.. we want to keep it organized. so I put the file name as multiple inheritance so the class name which contains the main method should also have the same name. “MultipleInheritance” so that we may not be confused and everything should be organized so i’ll just put the CHILD METHOD* here for now. uhh.. I mean child method, not child class void childMethod and in method, I’ll put something like This is a method from child. That’s it. Yeah, That’s enough so right now, we’ll have the actual class that has the main method hah that’s enough so we have public static void main(String args[ ]) and in here, I’ll create an object for the child class* i mean, the child class so.. let me say childObject Yeah, that’s good so first, I’ll call child object dot. childMethod so, yeah we’re just calling the child method which is over here for now and we’ll also call the parent method and see which one will execute so and as the child is Inheriting* both of them we have access to both of these methods so we don’t know which one of them will be executed or will it execute at all will it even compile in the first place let us see so childObject-dot- parentMethod. right? yep. alright, let me just try to compile this thing i have cmd cd.. work no, I’m sorry so cd>programming>java>tutorials yeah, we have the so we have javac this is to create the separate directory for the package we have and we also have an error so it doesn’t even take comma( , ) This is actually completely not possible even if you try with “and” it’s not possible if you try to put a comma and and again another extends it’s not possible or if you try to put this it’s not possible a multiple inheritance with classes is not possible in Java so this can be overcome by using interfaces. so interfaces are nothing but classes but with a little difference and that is the difference is, “there is no definition”. we cannot define any methods in here we can only declare it here or we cannot say what the method can do it can only say what methods it contain. so, uh… in this case, we have interfaces, right? so when we inherit a class, we put “extends”. but when we inherit an interface we use implements. alright. and one thing you have to remember is that we can execute this method or this program without defining these methods in this case we have one method in different classes we have to define this method and that should be done in the class that implemented those interfaces. in this case, the child class implemented the parent one and parent two. so those methods should be implemented or defined in the child class. so uhh.. parent method, right? let me say, System-dot-out print (I’m sorry) this is a method from parents but is defined by child class. it’s good. so is that it? Alright, let’s try. I don’t know why i removed it whatever programming java tutorials javac Alright, there are no errors now and package is created and we have all these parent one, parent two class, child, everyone ready to be executed. so let’s try javaexamples-dot-MultipleInheritance Bam! That’s it! so this is a method from child this one has been executed. very nice and method from parents but is defined by the child class. that means this one is also executed. that’s it, so you know… to have multiple inheritances, we have interface i hope you have understood and if there are anything i didn’t call you Mr. Co… or Miss. Cortana, or whatever you are Alright, so.. yeah, that’s it, if you have any kind of doubts in it you can always reach me through the comments Alright then Meet you later in the next video. Good bye 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top