WEBVTT

00:01.180 --> 00:01.780
Hi everyone.

00:01.780 --> 00:03.100
Welcome back to this tutorial.

00:03.100 --> 00:07.930
In this tutorial we will code and explore the multiple inheritance in Java.

00:07.960 --> 00:13.120
For this purpose, I'm going to create three interfaces and one implementation class.

00:13.150 --> 00:15.250
Let's create the first interface.

00:15.250 --> 00:20.650
I'm going to create the empty interfaces first and then we will write the default methods.

00:21.520 --> 00:22.720
I'm going to create the interface.

00:22.720 --> 00:25.450
I'm going to give it a name as interface one.

00:27.320 --> 00:27.710
There you go.

00:27.710 --> 00:29.680
It created the interface for you.

00:29.690 --> 00:35.390
Now I'm going to create the interface to I'm just copying that interface one class and then putting

00:35.390 --> 00:36.980
it in the default package.

00:36.980 --> 00:39.980
And then I'm going to rename that one as interface two.

00:40.220 --> 00:43.640
Similarly, I'm going to create the interface three to.

00:44.830 --> 00:46.200
Be right.

00:46.240 --> 00:46.780
There you go.

00:46.780 --> 00:48.970
We have all the interfaces created.

00:48.970 --> 00:54.130
Now, the next step, what I'm going to do, I'm going to create a default method in it.

00:57.270 --> 01:00.300
The method name is going to be default.

01:01.360 --> 01:02.310
Method A.

01:04.590 --> 01:07.230
And this is not going to return anything from it.

01:07.920 --> 01:08.380
Okay.

01:08.410 --> 01:10.380
Then this is going to just print.

01:13.180 --> 01:13.930
Right method.

01:16.610 --> 01:17.030
Okay.

01:18.780 --> 01:24.570
Similarly, I am going to create a method B and method C in interface 213.

01:25.290 --> 01:28.120
So I'm gonna go to the interface.

01:29.710 --> 01:30.070
To.

01:31.050 --> 01:31.620
Go there.

01:31.620 --> 01:34.560
And then let's use the method.

01:34.560 --> 01:39.330
This is going to be method B, This is going to be method B.

01:40.560 --> 01:41.850
Go to the interface three.

01:46.110 --> 01:46.830
The phase three.

01:47.580 --> 01:49.950
The interface three is going to have the method.

01:49.980 --> 01:50.430
See?

01:54.070 --> 01:54.470
There you go.

01:54.490 --> 01:57.060
Now we have all the interfaces ready.

01:57.070 --> 01:59.800
Let's go ahead and create the implementation class.

02:04.400 --> 02:08.150
We're going to create a implementation class called.

02:11.270 --> 02:11.690
Blind.

02:11.720 --> 02:12.920
One, two, three.

02:14.590 --> 02:16.690
Don't ask me why I gave that name.

02:16.690 --> 02:17.970
I'm just giving that name.

02:17.990 --> 02:21.070
You want to give some other meaningful name, please go ahead and do it.

02:21.100 --> 02:24.520
Now, this client, one, two, three is going to.

02:26.220 --> 02:30.150
Implement the three interfaces that we just created.

02:30.720 --> 02:34.650
Implement the phase one to phase two.

02:35.760 --> 02:37.320
And interface three.

02:39.250 --> 02:44.040
Okay, Now let's go ahead and implement this.

02:44.050 --> 02:48.430
Now let's go ahead and add the executable method, which is public static, void main.

02:48.790 --> 02:50.440
We have this ready Now.

02:50.440 --> 02:53.830
What we will do, we will create an instance of.

02:54.810 --> 02:55.920
9212, three.

02:55.950 --> 02:57.950
Equal to new client.

02:57.960 --> 02:58.890
One, two, three.

03:00.570 --> 03:05.130
Let's use the client one, two, three instance, and then we will check what are the methods it has

03:05.130 --> 03:05.730
access to.

03:05.760 --> 03:09.300
If we take a look at it, it has access to Method A.

03:10.950 --> 03:12.450
Take a look at the next method.

03:13.720 --> 03:15.280
As access to Method B.

03:16.320 --> 03:18.210
It has access to method C?

03:18.630 --> 03:22.800
Basically, this concept is called multiple inheritance.

03:22.800 --> 03:28.560
We are inheriting multiple behaviors from each and every interfaces.

03:30.650 --> 03:33.440
This was never possible before Java eight.

03:33.470 --> 03:37.640
Now let's go ahead and run this and then see what is getting printed in the console.

03:37.670 --> 03:38.080
There you go.

03:38.090 --> 03:47.540
It printed inside method A inside method B, I believe Interface three needs to change this one to interface

03:47.550 --> 03:48.920
inside method C.

03:50.100 --> 03:51.060
Run it again.

03:54.130 --> 03:54.680
There you go.

03:54.700 --> 03:59.470
Now, what we will do is I'm going to add some complexity into it.

03:59.500 --> 04:05.740
What I'm going to do, I'm going to have this interface to extends interface.

04:05.920 --> 04:06.580
One.

04:07.420 --> 04:07.660
Right.

04:07.660 --> 04:09.250
So we are building the relationship now.

04:09.250 --> 04:13.870
I'm going to go back to the implementation class and interface three.

04:15.750 --> 04:16.740
Extends.

04:17.620 --> 04:18.610
Interface to.

04:22.490 --> 04:22.850
There you go.

04:22.850 --> 04:24.970
So now we have the relationship.

04:24.980 --> 04:29.110
So interface one doesn't extend anything.

04:29.120 --> 04:39.080
I'm going to go back and interface two extends interface one and interface three extends interface two.

04:39.290 --> 04:41.050
So we have a relationship now.

04:41.060 --> 04:47.750
Now what I'm going to do is I'm going to override the method a implementation in interface two.

04:49.100 --> 04:52.940
Okay, so I'm going to go to the interface one and copy this.

04:55.830 --> 04:56.700
I'm an interface to.

04:56.700 --> 05:02.520
Now, as you notice, I'm going to put this implementation here and then what I'm going to do, I'm

05:02.520 --> 05:03.450
going to.

05:04.720 --> 05:08.920
Of the class that it is of the interface that it is getting called.

05:09.550 --> 05:13.960
With that class, I'm going to do the same thing for Interface one.

05:15.330 --> 05:16.500
A centrepiece.

05:17.700 --> 05:18.800
One class.

05:20.140 --> 05:24.910
Okay when it is going to print, the result is going to print along with the class that is actually

05:24.910 --> 05:29.290
along with the interface that is actually invoked during the runtime.

05:29.560 --> 05:34.210
Now, if I call the method, will it print?

05:35.440 --> 05:37.130
The lager.

05:37.370 --> 05:46.240
The stout, which is inside the method that is part of interface one or that is part of interface two.

05:46.240 --> 05:49.270
Basically, we are overriding the method A.

05:49.930 --> 05:52.780
Now let's go ahead and check which takes the preference.

05:54.010 --> 05:54.880
Going to run it.

05:58.020 --> 06:03.120
If you take a look at it, the result it printed the class as interface two.

06:03.150 --> 06:04.320
What does it mean?

06:04.320 --> 06:06.570
Is that Java in runtime?

06:06.570 --> 06:12.810
It is always going to resolve to the child implementation.

06:14.660 --> 06:18.050
So this is something which you have to keep in your mind.

06:19.590 --> 06:21.340
Now, let's complicate this.

06:21.360 --> 06:29.460
What I'm going to do, I'm going to have the default method implementation in the actual implementation

06:29.460 --> 06:30.310
class itself.

06:30.330 --> 06:36.930
So if we have it here, as we all know, we cannot have default keyword as part of the class.

06:36.990 --> 06:38.280
I'm going to change it to public.

06:38.940 --> 06:42.690
If I have the similar kind of implementation in the.

06:43.690 --> 06:46.360
Implementation class itself, which takes precedence.

06:46.540 --> 06:48.430
Now, let's go ahead and run this one.

06:49.100 --> 06:54.830
If I run this, if we take a look at the result in the console, it printed the value as client one,

06:54.830 --> 06:55.550
two, three.

06:55.580 --> 07:03.560
Basically, the class that overrides the default method takes precedence over any other implementation

07:03.560 --> 07:07.100
that we have in other interfaces.

07:07.250 --> 07:11.690
Let me summarize on the order of resolving the method call during runtime.

07:12.200 --> 07:13.400
Number one is.

07:14.620 --> 07:17.350
Class that implements the interface.

07:20.220 --> 07:21.660
And number two.

07:23.080 --> 07:25.420
Us the Subinterface.

07:27.400 --> 07:36.070
Dot extends the interface, assuming both the methods, both the class and interface have the default

07:36.070 --> 07:38.800
method overridden from its parent interface.

07:39.500 --> 07:43.970
So this is something which you have to keep in your mind when you are.

07:44.820 --> 07:51.810
Overriding the default method in your subinterface or in your implementation class of that interface.

07:52.480 --> 07:52.810
But this.

07:52.810 --> 07:54.370
We came to the end of this tutorial.

07:54.400 --> 07:55.540
Thank you for watching.
