Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the multiple inheritance in Java.
For this purpose, I'm going to create three interfaces and one implementation class.
Let's create the first interface.
I'm going to create the empty interfaces first and then we will write the default methods.
I'm going to create the interface.
I'm going to give it a name as interface one.
There you go.
It created the interface for you.
Now I'm going to create the interface to I'm just copying that interface one class and then putting
it in the default package.
And then I'm going to rename that one as interface two.
Similarly, I'm going to create the interface three to.
Be right.
There you go.
We have all the interfaces created.
Now, the next step, what I'm going to do, I'm going to create a default method in it.
The method name is going to be default.
Method A.
And this is not going to return anything from it.
Okay.
Then this is going to just print.
Right method.
Okay.
Similarly, I am going to create a method B and method C in interface 213.
So I'm gonna go to the interface.
To.
Go there.
And then let's use the method.
This is going to be method B, This is going to be method B.
Go to the interface three.
The phase three.
The interface three is going to have the method.
See?
There you go.
Now we have all the interfaces ready.
Let's go ahead and create the implementation class.
We're going to create a implementation class called.
Blind.
One, two, three.
Don't ask me why I gave that name.
I'm just giving that name.
You want to give some other meaningful name, please go ahead and do it.
Now, this client, one, two, three is going to.
Implement the three interfaces that we just created.
Implement the phase one to phase two.
And interface three.
Okay, Now let's go ahead and implement this.
Now let's go ahead and add the executable method, which is public static, void main.
We have this ready Now.
What we will do, we will create an instance of.
9212, three.
Equal to new client.
One, two, three.
Let's use the client one, two, three instance, and then we will check what are the methods it has
access to.
If we take a look at it, it has access to Method A.
Take a look at the next method.
As access to Method B.
It has access to method C?
Basically, this concept is called multiple inheritance.
We are inheriting multiple behaviors from each and every interfaces.
This was never possible before Java eight.
Now let's go ahead and run this and then see what is getting printed in the console.
There you go.
It printed inside method A inside method B, I believe Interface three needs to change this one to interface
inside method C.
Run it again.
There you go.
Now, what we will do is I'm going to add some complexity into it.
What I'm going to do, I'm going to have this interface to extends interface.
One.
Right.
So we are building the relationship now.
I'm going to go back to the implementation class and interface three.
Extends.
Interface to.
There you go.
So now we have the relationship.
So interface one doesn't extend anything.
I'm going to go back and interface two extends interface one and interface three extends interface two.
So we have a relationship now.
Now what I'm going to do is I'm going to override the method a implementation in interface two.
Okay, so I'm going to go to the interface one and copy this.
I'm an interface to.
Now, as you notice, I'm going to put this implementation here and then what I'm going to do, I'm
going to.
Of the class that it is of the interface that it is getting called.
With that class, I'm going to do the same thing for Interface one.
A centrepiece.
One class.
Okay when it is going to print, the result is going to print along with the class that is actually
along with the interface that is actually invoked during the runtime.
Now, if I call the method, will it print?
The lager.
The stout, which is inside the method that is part of interface one or that is part of interface two.
Basically, we are overriding the method A.
Now let's go ahead and check which takes the preference.
Going to run it.
If you take a look at it, the result it printed the class as interface two.
What does it mean?
Is that Java in runtime?
It is always going to resolve to the child implementation.
So this is something which you have to keep in your mind.
Now, let's complicate this.
What I'm going to do, I'm going to have the default method implementation in the actual implementation
class itself.
So if we have it here, as we all know, we cannot have default keyword as part of the class.
I'm going to change it to public.
If I have the similar kind of implementation in the.
Implementation class itself, which takes precedence.
Now, let's go ahead and run this one.
If I run this, if we take a look at the result in the console, it printed the value as client one,
two, three.
Basically, the class that overrides the default method takes precedence over any other implementation
that we have in other interfaces.
Let me summarize on the order of resolving the method call during runtime.
Number one is.
Class that implements the interface.
And number two.
Us the Subinterface.
Dot extends the interface, assuming both the methods, both the class and interface have the default
method overridden from its parent interface.
So this is something which you have to keep in your mind when you are.
Overriding the default method in your subinterface or in your implementation class of that interface.
But this.
We came to the end of this tutorial.
Thank you for watching.