Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code a simple interface and then we will try to add default methods and static
methods to that interface.
The first step is I'm going to create an interface called multiply.
That interface purpose is to perform the multiplication of the list that is being passed as an input.
Want to create the interface in the class selection, I will change it to interface and then the interface
name is going to be.
Multiplier.
There you go.
We have the interface created.
The next step is I'm going to create an implementation class of this interface.
I'm going to create a class.
The class name is going to be multiplier.
And this is going to implement the multiplayer interface.
There you go.
We have the implementation class and interface ready.
Now let's go ahead and introduce all the introduce all the new functionalities.
So the first step is.
Multiply is a method.
It is going to perform the multiplication of the integer list.
List of integer.
I'm going to give it a name called Integer List.
So this is a contract, right?
We are not coding the default method and static method set.
This multiplier has one single method called multiply, and then this is going to take in a input list
and return the input as a int as an output.
Now let's go back to the.
Multiplier, which is the implementation class of this one.
Now, we have a compilation issue here.
Let's go ahead and check what's happening there.
If you take a look at it, it is going to ask you to implement the method.
There you go.
So now we have to implement that method.
As you see this override method, basically this means we are overriding something from this class.
Okay?
From this interface actually.
So the interface has a multiply method and we are going to implement the logic how you want to perform
the multiplication.
Let's go ahead and perform the.
Implementation logic.
So in here I'm going to use the.
Stream method.
Dot stream.
And then we are going to use a reduce method.
If you remember from the prior tutorials, reduce method is handy in order to perform the.
Multiplication operation.
I'm going to do X, Asterix Y.
So this is going to perform the multiplication of the input list and then return you the result.
So I'm going to remove this return.
And see what's happening here.
It's going to give also, since we did not provide the default value, it is returning the optional.
I'm going to have the default value as one.
So whenever it performs the multiplication, it's going to start with one and then all the values that
is being passed from the integer list.
Now the next step is I'm going to create a client class that is going to call this implementation class
and pass the input.
Now.
With another class.
The class name is going to be.
Multiplayer.
Later.
There you go.
I'm going to make this class executable by adding public static void main method.
Now the next step is I'm going to make this class.
Let's put this in presentation mode.
Now I'm going to create an instance of multiplayer.
So multiplayer is a interface.
I'm going to give it a name multiplayer and then new multiplayer, which is the implementation class.
And this is allowed.
Let's create the input list.
List of integers and then it's going to be integer list.
I'm going to keep this example really simple.
I'm going to use arrays dot as list.
I'm going to give the values as one comma, three, comma five.
So the result is going to be 15.
That's the result which this multiplayer implementation class is going to return.
Now let's call this.
So the result is you're going to call the multiplier, you're going to use the multiplier instance dot
multiply.
There you see, this is a method which we implemented in the multiplier class.
Now I'm going to pass this integer list.
Let's run this and check the result.
There you go.
It gave you the result as 15.
So nothing new until this point.
This is something which you can do it before Java eight also.
Now let's start introducing the default method and static methods.
So default methods what I'm going to do.
I'm going to go to this multiplayer interface, so I'm going to do this in here.
I'm going to create a method called size that is going to give you the size of the input list.
That's what it is going to do.
What I'm going to do, I'm going to create the default method.
The first step is we are going to have the default keyword and then what is that?
It is going to return.
It is going to return the size of the input list.
So the size of the input list is going to be an integer.
And then if the size method.
So I'm creating this size method in the multiplayer interface.
So this is going to accept the list of.
Integer.
Want to give it a name?
And what is that?
It is going to return.
This is going to return.
Integer list.
Dot size.
Okay, we're good, right?
So we have successfully implemented the default method in the interface.
Now how can we call that?
So when to have this code command as a default method.
Now, remember?
Can use the same multiplayer interface when we call.
Before we just had the multiply method, right?
Now let's see, what are the other what are the methods that are available?
We take a look at it.
It has another method called size.
So this is a method which we just coded.
I'm going to call that method and then I'm going to pass this integer list.
Good.
Now let's go ahead and run this one.
We take a look at it, it returned you the result as three.
So if you compare this with the prior Java eight version.
Let's go back to the interface.
In the interface, we have introduced this new functionality.
This was never possible before Java eight.
Basically, you could have created another, you could have declared another method called sys, but
the impact would be you have to go to the multiplayer implementation class and then implement the logic.
So the logic, whatever we had implemented here in the interface needs to be implemented only in multiplayer
class prior to Java eight.
So that's the advantage of default methods.
We can still add functionalities to the list interface, which is the multiplayer, not the list interface,
the multiplayer interface and not affecting any of the classes that already implements this interface.
This is one handy way of enhancing an interface.
Now let's talk about the static methods.
So I'm going to create another method called static.
And then this method is going to return me a boolean if the list is not empty.
That's what it is going to do.
So I'm going to create a method called is empty and this is going to accept a list of.
Integer.
Okay.
I'll give it a name integer list.
So what is that?
It is going to return.
This is going to return.
If the integer list is not null.
And.
Integer less dot size.
Equal to equal to zero.
Basically, this is going to size greater than zero, right?
So if the list is empty, then this will return false.
If the list is not empty, then this will return.
True.
Now, let's go ahead and check it.
Let's try to access the new static method using the.
Multiplayer instance.
You see the isEmpty method here?
No, because it's a static method.
You can only access a static method using the class.
Right.
Basically in here, using the interface, I'm going to give the comment as static method is empty is.
And we are going to use the interface itself because it's a static method.
You can only access the static method using the.
Actual interface itself.
And then you get access to that use empty method.
And let's pass this one.
So this is going to return the value as true because it checks the implement.
If we take a look at the implementation, it checks the integer is not equal to null and an integer
list dot size greater than zero.
But our input list, if you take a look at it, has three elements.
So we are going to get the result as true.
Here you go.
It gave you the result as true.
Right.
So let's go ahead and go back to the interface.
What is the difference between the two in that case?
Right.
If you remember from the presentation, default methods can be overridden, but the static methods.
Cannot be overwritten.
Now the implementation class of the multiplayer interface is multiplayer.
I'm going to open the class.
I'm going to copy this method, and then I'm going to go to the multiplayer class, as you see here.
And then I'm going to overwrite that functionality.
You cannot use default methods in the context of a class.
So I'm going to give it a access modifier.
And then the functionality is similar to the multiplier interface, but we are overriding it, right?
In order to show the difference what I'm going to do, I'm going to add a sort.
Want to give the code commands as inside.
Multiplier.
Interface.
I'm going to copy it.
I'm going to go back to the multiplayer pool.
In here.
I'm going to give inside multiplier class.
Just to show the difference between which method is getting invoked.
So basically what we have done here, we have the multiplayer interface, which has the default method,
and then we are trying to override the similar functionality.
You should match the method signature, meaning the parameters, the return type and the method name
should all be the same.
That's what we have here in the multiplayer class.
Now we will call the size method and then see which.
Implementation gets invoked.
Right.
I'm going to go to the multiplayer client class.
Multiplayer client.
There you go.
Let's run this and check the result.
So the size method is going to invoke either the method implementation in the interface or the implementation
class.
Let's run this and check the result.
If you take a look at the result it implemented inside multiplier class, so on runtime, when it is
trying to resolve the method, it resolves the multipliers multiplier plus size method.
That's the one of that's one of the key feature that you have to keep in your mind whenever you override
a default method.
Then the the class that overrides that functionality is the one which is going to get invoked.
So let's go back to the difference between the default and static methods.
Static methods.
We cannot override it because this is at the class level.
Basically, in order to access this method, we use the interface and then we will call the isEmpty
method.
Okay, so this is something which we have to be really, really careful about.
Whenever you implement the same default method in the implementation class, then that is going to take
the precedence and during runtime.
That is going to get invoked.
With this.
We came to the end of this tutorial.
Thank you for watching.