As we all know before Java 8 the interface is the only thing that was purely abstract. It means you were not able to define concrete methods inside the interface. Now interfaces are no longer as 100% pure abstract. According to the Java 8 interface definition, you can define concrete methods inside the interface. Java 8 allows you to define default and static methods inside the interface.
What are the default and static methods?
The default method declares with the default keyword at the beginning of the method and having an implementation body. Default methods are only allowed inside the interface.
The static method is just like the method present at the class level. A method declares with the static keyword at the beginning of the method and having an implementation body. Below is an example of default and static method.
One thing you should always remember that anything defines inside the interface by default public and variables present inside the interface are implicitly static and final.
Default and Static methods wrt Inheritance
As we know already an interface can extend one or more than one interface at a time. Now in the case of the interface, there are two situations that come up. The first one is when the child interface extends a single interface and the second one is when the child interface extends more than one interface at a time. Let's take an example as the first situation is very simple there are no loopholes.
Now in the second situation when an interface or a class extends more than one interface then there two cases come up. The first case is if both parent interface having an exact same default method and the second one is if both parent interface having different default method. Let's suppose a class or an interface implements or extends two or more interfaces. Now the implemented class or interface would inherit default methods from both parent interfaces. At this point in the first case, multiple inheritance problems would occur and also known as a diamond problem. Here is an example.
Parent interfaces: HorrorMovies.java and ThrillerMovies.java
Child interface: HorrorThrillerMovies.java
In the above example, if you would comment a method getMovies() then the java compiler will show you a compilation error "Duplicate default methods named getMovies with the parameters () and () are inherited from the types ThrillerMovies and HorrorMovies". Hence the java compiler ended up with the ambiguity problem or you can say Diamond problem.
To resolve this problem you must provide the implementation explicitly to the default methods present in both parent interfaces in your implemented class or interface. By providing implementation you are resolving ambiguity problems and overriding the implementation of default methods of parent interfaces.
Now let's talk about static methods inside the interface. The first thing you should know about static methods is that static methods are not part of the inheritance concept. When you declared the exact same static method in the child class or interface with the new implementation then this is called method hiding.
In Java 8, two interfaces can have the exact same static methods. When a child interface or a class extends or implements two or more than two interfaces having the exact same static method then the java compiler does not show any error like it does in case of default methods. Let's take an example.
Parent interfaces: Hollywood.java and Bollywood.java
Child interface: Tollywood.java
In the above example, if you would comment code inside the Tollywood interface then the java compiler will not show any compilation error like it does in case of default methods.
Now I am ending up this post and will update it if I will find something new about default and static methods. Also, you can share your queries and feedback in the comment section below, and if you find something new about this topic do comment below.
Soon I will come up with the post on the topics inheritance, method hiding in detail. Keep study guys and share with your friends and colleague also. Thank you so much.
Comments
Post a Comment