Java 8 introduced default and static methods in interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract of the implementing classes.

Problem Statement: Defining and finalizing interfaces have been tough job. If any changes in interface i.e. Add Method, Remove Method, Change Method, it will require change in all implementing classes.

As interface grows old, the number of classes implementing it might grow to an extent that it’s not possible to extend interfaces. That’s why when designing an application, most of the frameworks provide a base implementation class and then we extend it and override methods that are applicable for our application.

Java Interface Default Method

We need to use “default” keyword with the method signature. For example,

printLog(String str) is the default method in the Interface1. When a class will implement InterfaceA, it is not mandatory to provide implementation for default methods of interface.

This feature will help us in extending interfaces with additional methods, all we need is to provide a default implementation.

Problem Arisen: Java doesn’t allow us to extend multiple classes because it will result in the “Diamond Problem” where compiler can’t decide which superclass method to use.

With the default methods, the diamond problem would arise for interfaces too. Because if a class is implementing both InterfaceA and InterfaceB and doesn’t implement the common default method, compiler can’t decide which one to chose.

If a class is implementing both the above interfaces, it will have to provide implementation for printLog() method otherwise compiler will throw compile time error.

Problem Solved: You can choose specific implementation by implementing printLog() method of specific interface.

Key Points
  1. Java interface default methods will help us in extending interfaces without having the fear of breaking implementation classes.
  2. Default methods has differenciated interfaces and abstract classes.
  3. We can provide default implementation and the implementation classes can chose which one to override.
  4. Java interface default methods are also referred to as Defender Methods or Virtual extension methods.
Java Interface Static Method

Java interface static method is similar to default method except that we can’t override them in the implementation classes.

Why required? It helps us in avoiding undesired results incase of poor implementation in implementation classes.

Output:

called method staticPrintLog() of InterfaceA called method staticPrintLog() of StaticMethodImplementor

You’re not truly overriding the static interface method. In other words, the static method can only be called through the interface or class type reference, not an instance variable.

Key Points
  1. Java interface static method is part of interface, we can’t use it for implementation class objects.
  2. Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *