Hi everyone.
Welcome back to this tutorial.
In this tutorial, I'm going to explain some theory about the default and static methods in interfaces.
Let's talk about what was allowed before Java eight in interfaces.
First, the concept of interfaces is to just define the contract, meaning you are just allowed to declare
the method, but you cannot have any method implementation in interfaces.
The class that implements the interface should implement the logic for the declared method in the interface.
We all know that this limitation had a severe impact for an interface to evolve or upgrade because if
we try to declare new methods in the interfaces, then the class that already implements this interface
should implement the new method.
Otherwise this will lead to compilation issues.
The main impact because of this is for the library creators.
It was really hard to introduce the new functionality parties in the interfaces.
In order to avoid this, the Java creators made the change to allow the method implementation in interfaces
itself via static and default methods.
Let's talk about the default method first.
Default methods in interfaces will have the new default keyword as part of the method signature itself,
which then registers that method as a default method in the interface.
It is an example of a default method from the list interface.
This is a short method which is going to sort the list collection.
If you notice here it has the default keyword as part of the method signature.
With that, the Java compiler will allow the method implementation in interfaces prior to Java eight.
We normally use the collections class to perform the similar operation.
If all methods can be overridden in the implementation class, we will explore all those options in
our future tutorials.
If all methods are going to help the Java developers to introduce the new functionalities in the future
without having any impact to the current classes that implements the interface.
Similar to default methods.
We have static methods also allowed as part of the interfaces.
The only difference between the two meaning between the static method and the default method.
The static method cannot be overridden in the implementation classes.
Let's go ahead and explore all these new methods via code in the following tutorials.
With this we came to the end of this tutorial.
Thank you for watching.