WEBVTT

00:00.740 --> 00:01.340
Hi everyone.

00:01.340 --> 00:02.660
Welcome back to this tutorial.

00:02.660 --> 00:09.170
In this tutorial we will code a simple interface and then we will try to add default methods and static

00:09.170 --> 00:11.000
methods to that interface.

00:11.030 --> 00:14.300
The first step is I'm going to create an interface called multiply.

00:14.330 --> 00:21.080
That interface purpose is to perform the multiplication of the list that is being passed as an input.

00:22.120 --> 00:30.160
Want to create the interface in the class selection, I will change it to interface and then the interface

00:30.160 --> 00:31.630
name is going to be.

00:32.550 --> 00:33.450
Multiplier.

00:33.840 --> 00:34.380
There you go.

00:34.380 --> 00:36.120
We have the interface created.

00:36.150 --> 00:41.310
The next step is I'm going to create an implementation class of this interface.

00:41.310 --> 00:42.900
I'm going to create a class.

00:42.900 --> 00:46.380
The class name is going to be multiplier.

00:49.200 --> 00:53.670
And this is going to implement the multiplayer interface.

00:53.700 --> 00:54.330
There you go.

00:54.330 --> 00:57.510
We have the implementation class and interface ready.

00:57.540 --> 01:02.730
Now let's go ahead and introduce all the introduce all the new functionalities.

01:04.090 --> 01:05.200
So the first step is.

01:06.510 --> 01:08.370
Multiply is a method.

01:09.950 --> 01:14.630
It is going to perform the multiplication of the integer list.

01:15.170 --> 01:16.370
List of integer.

01:17.090 --> 01:19.340
I'm going to give it a name called Integer List.

01:20.790 --> 01:22.590
So this is a contract, right?

01:22.620 --> 01:25.800
We are not coding the default method and static method set.

01:25.830 --> 01:33.180
This multiplier has one single method called multiply, and then this is going to take in a input list

01:33.180 --> 01:36.030
and return the input as a int as an output.

01:36.060 --> 01:37.980
Now let's go back to the.

01:40.550 --> 01:45.110
Multiplier, which is the implementation class of this one.

01:45.140 --> 01:47.840
Now, we have a compilation issue here.

01:48.140 --> 01:49.910
Let's go ahead and check what's happening there.

01:50.580 --> 01:54.990
If you take a look at it, it is going to ask you to implement the method.

01:55.020 --> 01:55.770
There you go.

01:55.800 --> 01:58.950
So now we have to implement that method.

01:58.950 --> 02:05.250
As you see this override method, basically this means we are overriding something from this class.

02:05.430 --> 02:05.730
Okay?

02:05.730 --> 02:07.020
From this interface actually.

02:07.020 --> 02:12.180
So the interface has a multiply method and we are going to implement the logic how you want to perform

02:12.180 --> 02:13.410
the multiplication.

02:14.200 --> 02:16.720
Let's go ahead and perform the.

02:17.740 --> 02:18.970
Implementation logic.

02:19.510 --> 02:22.390
So in here I'm going to use the.

02:23.620 --> 02:24.610
Stream method.

02:25.920 --> 02:26.910
Dot stream.

02:27.240 --> 02:29.520
And then we are going to use a reduce method.

02:29.520 --> 02:35.910
If you remember from the prior tutorials, reduce method is handy in order to perform the.

02:36.990 --> 02:38.850
Multiplication operation.

02:38.940 --> 02:41.430
I'm going to do X, Asterix Y.

02:41.850 --> 02:47.670
So this is going to perform the multiplication of the input list and then return you the result.

02:48.060 --> 02:49.710
So I'm going to remove this return.

02:50.680 --> 02:53.350
And see what's happening here.

02:53.530 --> 02:59.950
It's going to give also, since we did not provide the default value, it is returning the optional.

02:59.950 --> 03:02.700
I'm going to have the default value as one.

03:02.710 --> 03:07.840
So whenever it performs the multiplication, it's going to start with one and then all the values that

03:07.840 --> 03:10.750
is being passed from the integer list.

03:10.780 --> 03:17.350
Now the next step is I'm going to create a client class that is going to call this implementation class

03:17.350 --> 03:18.940
and pass the input.

03:19.180 --> 03:20.080
Now.

03:21.610 --> 03:22.870
With another class.

03:23.470 --> 03:25.150
The class name is going to be.

03:26.770 --> 03:27.910
Multiplayer.

03:30.470 --> 03:30.850
Later.

03:33.930 --> 03:34.280
There you go.

03:34.290 --> 03:38.670
I'm going to make this class executable by adding public static void main method.

03:38.700 --> 03:41.760
Now the next step is I'm going to make this class.

03:42.330 --> 03:44.190
Let's put this in presentation mode.

03:46.190 --> 03:49.480
Now I'm going to create an instance of multiplayer.

03:49.490 --> 03:51.830
So multiplayer is a interface.

03:52.160 --> 03:59.300
I'm going to give it a name multiplayer and then new multiplayer, which is the implementation class.

03:59.300 --> 04:00.530
And this is allowed.

04:00.560 --> 04:02.300
Let's create the input list.

04:02.330 --> 04:08.360
List of integers and then it's going to be integer list.

04:10.920 --> 04:14.040
I'm going to keep this example really simple.

04:14.280 --> 04:17.780
I'm going to use arrays dot as list.

04:17.880 --> 04:22.110
I'm going to give the values as one comma, three, comma five.

04:22.140 --> 04:24.330
So the result is going to be 15.

04:24.360 --> 04:28.950
That's the result which this multiplayer implementation class is going to return.

04:28.980 --> 04:30.510
Now let's call this.

04:32.530 --> 04:40.960
So the result is you're going to call the multiplier, you're going to use the multiplier instance dot

04:40.960 --> 04:41.630
multiply.

04:41.650 --> 04:47.180
There you see, this is a method which we implemented in the multiplier class.

04:47.200 --> 04:49.660
Now I'm going to pass this integer list.

04:50.260 --> 04:51.970
Let's run this and check the result.

04:54.790 --> 04:55.150
There you go.

04:55.150 --> 04:57.420
It gave you the result as 15.

04:57.430 --> 04:59.640
So nothing new until this point.

04:59.650 --> 05:03.700
This is something which you can do it before Java eight also.

05:03.700 --> 05:08.800
Now let's start introducing the default method and static methods.

05:08.800 --> 05:11.320
So default methods what I'm going to do.

05:12.290 --> 05:17.280
I'm going to go to this multiplayer interface, so I'm going to do this in here.

05:17.290 --> 05:25.580
I'm going to create a method called size that is going to give you the size of the input list.

05:25.600 --> 05:27.010
That's what it is going to do.

05:27.100 --> 05:30.720
What I'm going to do, I'm going to create the default method.

05:30.730 --> 05:35.950
The first step is we are going to have the default keyword and then what is that?

05:35.950 --> 05:36.820
It is going to return.

05:36.820 --> 05:39.820
It is going to return the size of the input list.

05:40.180 --> 05:44.140
So the size of the input list is going to be an integer.

05:44.140 --> 05:46.930
And then if the size method.

05:46.930 --> 05:51.970
So I'm creating this size method in the multiplayer interface.

05:51.970 --> 05:54.760
So this is going to accept the list of.

05:55.670 --> 05:56.510
Integer.

05:58.860 --> 05:59.900
Want to give it a name?

06:00.750 --> 06:01.500
And what is that?

06:01.500 --> 06:02.490
It is going to return.

06:02.490 --> 06:04.440
This is going to return.

06:07.290 --> 06:08.280
Integer list.

06:08.280 --> 06:09.450
Dot size.

06:11.430 --> 06:13.530
Okay, we're good, right?

06:13.530 --> 06:19.830
So we have successfully implemented the default method in the interface.

06:20.130 --> 06:21.930
Now how can we call that?

06:24.510 --> 06:28.620
So when to have this code command as a default method.

06:31.460 --> 06:33.350
Now, remember?

06:34.500 --> 06:36.650
Can use the same multiplayer interface when we call.

06:36.680 --> 06:40.250
Before we just had the multiply method, right?

06:40.280 --> 06:43.400
Now let's see, what are the other what are the methods that are available?

06:45.020 --> 06:45.750
We take a look at it.

06:45.770 --> 06:48.500
It has another method called size.

06:48.920 --> 06:51.500
So this is a method which we just coded.

06:51.800 --> 06:55.670
I'm going to call that method and then I'm going to pass this integer list.

06:56.840 --> 06:57.260
Good.

06:57.260 --> 06:58.910
Now let's go ahead and run this one.

07:00.830 --> 07:03.660
We take a look at it, it returned you the result as three.

07:03.680 --> 07:08.570
So if you compare this with the prior Java eight version.

07:09.980 --> 07:11.200
Let's go back to the interface.

07:11.210 --> 07:14.380
In the interface, we have introduced this new functionality.

07:14.390 --> 07:17.180
This was never possible before Java eight.

07:17.210 --> 07:22.850
Basically, you could have created another, you could have declared another method called sys, but

07:22.880 --> 07:28.940
the impact would be you have to go to the multiplayer implementation class and then implement the logic.

07:28.940 --> 07:36.620
So the logic, whatever we had implemented here in the interface needs to be implemented only in multiplayer

07:37.130 --> 07:38.810
class prior to Java eight.

07:38.840 --> 07:41.420
So that's the advantage of default methods.

07:41.420 --> 07:47.720
We can still add functionalities to the list interface, which is the multiplayer, not the list interface,

07:47.720 --> 07:54.680
the multiplayer interface and not affecting any of the classes that already implements this interface.

07:54.830 --> 07:58.820
This is one handy way of enhancing an interface.

07:58.820 --> 08:01.400
Now let's talk about the static methods.

08:01.400 --> 08:05.090
So I'm going to create another method called static.

08:05.850 --> 08:10.980
And then this method is going to return me a boolean if the list is not empty.

08:11.490 --> 08:12.760
That's what it is going to do.

08:12.780 --> 08:19.680
So I'm going to create a method called is empty and this is going to accept a list of.

08:20.350 --> 08:21.070
Integer.

08:21.820 --> 08:22.200
Okay.

08:22.210 --> 08:24.130
I'll give it a name integer list.

08:24.340 --> 08:25.270
So what is that?

08:25.270 --> 08:26.200
It is going to return.

08:26.200 --> 08:28.000
This is going to return.

08:28.810 --> 08:32.650
If the integer list is not null.

08:34.260 --> 08:35.070
And.

08:36.420 --> 08:38.760
Integer less dot size.

08:41.930 --> 08:42.860
Equal to equal to zero.

08:42.860 --> 08:46.510
Basically, this is going to size greater than zero, right?

08:47.360 --> 08:52.250
So if the list is empty, then this will return false.

08:52.250 --> 08:55.220
If the list is not empty, then this will return.

08:55.340 --> 08:56.000
True.

08:56.390 --> 08:58.490
Now, let's go ahead and check it.

08:59.220 --> 09:02.970
Let's try to access the new static method using the.

09:03.420 --> 09:04.890
Multiplayer instance.

09:05.700 --> 09:08.720
You see the isEmpty method here?

09:08.900 --> 09:11.000
No, because it's a static method.

09:11.000 --> 09:15.490
You can only access a static method using the class.

09:15.500 --> 09:16.100
Right.

09:16.130 --> 09:23.720
Basically in here, using the interface, I'm going to give the comment as static method is empty is.

09:24.770 --> 09:29.260
And we are going to use the interface itself because it's a static method.

09:29.270 --> 09:31.760
You can only access the static method using the.

09:32.770 --> 09:34.180
Actual interface itself.

09:34.180 --> 09:37.450
And then you get access to that use empty method.

09:38.340 --> 09:39.530
And let's pass this one.

09:39.540 --> 09:43.720
So this is going to return the value as true because it checks the implement.

09:43.740 --> 09:48.790
If we take a look at the implementation, it checks the integer is not equal to null and an integer

09:48.790 --> 09:50.550
list dot size greater than zero.

09:51.200 --> 09:53.930
But our input list, if you take a look at it, has three elements.

09:53.930 --> 09:56.660
So we are going to get the result as true.

09:58.100 --> 09:58.530
Here you go.

09:58.550 --> 10:00.260
It gave you the result as true.

10:01.110 --> 10:01.470
Right.

10:01.470 --> 10:04.310
So let's go ahead and go back to the interface.

10:04.320 --> 10:06.330
What is the difference between the two in that case?

10:06.330 --> 10:06.990
Right.

10:08.770 --> 10:15.070
If you remember from the presentation, default methods can be overridden, but the static methods.

10:15.790 --> 10:17.030
Cannot be overwritten.

10:17.050 --> 10:22.670
Now the implementation class of the multiplayer interface is multiplayer.

10:23.220 --> 10:25.150
I'm going to open the class.

10:25.180 --> 10:32.230
I'm going to copy this method, and then I'm going to go to the multiplayer class, as you see here.

10:33.080 --> 10:35.840
And then I'm going to overwrite that functionality.

10:36.530 --> 10:40.180
You cannot use default methods in the context of a class.

10:40.230 --> 10:42.980
So I'm going to give it a access modifier.

10:42.980 --> 10:51.790
And then the functionality is similar to the multiplier interface, but we are overriding it, right?

10:51.800 --> 10:55.460
In order to show the difference what I'm going to do, I'm going to add a sort.

10:56.420 --> 10:59.540
Want to give the code commands as inside.

11:00.820 --> 11:02.020
Multiplier.

11:03.210 --> 11:04.200
Interface.

11:05.400 --> 11:06.570
I'm going to copy it.

11:06.750 --> 11:09.640
I'm going to go back to the multiplayer pool.

11:10.660 --> 11:11.500
In here.

11:11.500 --> 11:16.180
I'm going to give inside multiplier class.

11:17.190 --> 11:20.640
Just to show the difference between which method is getting invoked.

11:20.640 --> 11:26.970
So basically what we have done here, we have the multiplayer interface, which has the default method,

11:26.970 --> 11:31.790
and then we are trying to override the similar functionality.

11:31.800 --> 11:37.860
You should match the method signature, meaning the parameters, the return type and the method name

11:37.860 --> 11:39.390
should all be the same.

11:39.390 --> 11:43.080
That's what we have here in the multiplayer class.

11:43.110 --> 11:48.390
Now we will call the size method and then see which.

11:49.460 --> 11:51.080
Implementation gets invoked.

11:51.890 --> 11:52.310
Right.

11:52.430 --> 11:55.580
I'm going to go to the multiplayer client class.

11:56.890 --> 11:58.150
Multiplayer client.

11:58.180 --> 11:58.900
There you go.

11:59.200 --> 12:01.670
Let's run this and check the result.

12:01.690 --> 12:10.600
So the size method is going to invoke either the method implementation in the interface or the implementation

12:10.600 --> 12:11.170
class.

12:11.500 --> 12:13.000
Let's run this and check the result.

12:13.300 --> 12:21.820
If you take a look at the result it implemented inside multiplier class, so on runtime, when it is

12:21.820 --> 12:29.560
trying to resolve the method, it resolves the multipliers multiplier plus size method.

12:29.590 --> 12:36.340
That's the one of that's one of the key feature that you have to keep in your mind whenever you override

12:36.340 --> 12:37.380
a default method.

12:37.390 --> 12:43.990
Then the the class that overrides that functionality is the one which is going to get invoked.

12:45.320 --> 12:48.890
So let's go back to the difference between the default and static methods.

12:48.920 --> 12:49.760
Static methods.

12:49.760 --> 12:53.900
We cannot override it because this is at the class level.

12:53.930 --> 12:59.750
Basically, in order to access this method, we use the interface and then we will call the isEmpty

12:59.750 --> 13:00.380
method.

13:00.950 --> 13:05.120
Okay, so this is something which we have to be really, really careful about.

13:05.150 --> 13:14.990
Whenever you implement the same default method in the implementation class, then that is going to take

13:14.990 --> 13:17.150
the precedence and during runtime.

13:18.220 --> 13:19.780
That is going to get invoked.

13:21.430 --> 13:21.730
With this.

13:21.730 --> 13:23.150
We came to the end of this tutorial.

13:23.170 --> 13:24.190
Thank you for watching.
