Java 8 allows to define default method in interface. Default methods allow us to add new methods in interface that are automatically available to it’s implementation classes by default. This is called method extension in Interfaces.
1. default method in interface
- Until 1.7 version inside interface we can declare only public abstract methods and public static final variables, concrete methods are not allowed.
- But from Java 1.8 version on wards in addition to these, we can declare default concrete methods also inside interface, which are also known as defender methods.
- Default methods are allowed only in interfaces.
- All method declarations in an interface, including default methods, are implicitly
public
, so you can omit thepublic
modifier.
We can declare default method with the keyword default
as follows:
public interface MyInterface { //default method default void wish(String user) { System.out.println("Hello "+user+"!"); } }
2. Use of default method in java interfaces
1. Any class that implements an interface must provide an implementation for each method defined by the interface or inherit the implementation from a super class, but default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface. Default methods also known as defender methods or virtual extension methods.
2. The main advantage of default methods is without effecting implementation classes we can add new functionality to the interface (backward compatibility).
3. Interface default methods are by default available to all implementation classes. An implementation class can use these default methods directly or can override. When do override default methods in implementation classes you omit the default keyword.
2.1. Example 1
interface MyInterface { default void wish(String user) { System.out.println("Hello "+user+"!"); } }
public class DefaultMethodDemo1 implements MyInterface { public static void main(String[] args) { DefaultMethodDemo demo = new DefaultMethodDemo(); demo.wish("Peter"); // Hello Peter! } }
2.2. Example 2 overriding default method
Following example shows you define java default method in interface and overriding it implementation class.
public class DefaultMethodDemo2 implements MyInterface { @Override public void wish(String user) { System.out.println("Welcome "+ user+"!"); } public static void main(String[] args) { DefaultMethodDemo demo = new DefaultMethodDemo(); demo.wish("Peter"); // Welcome Peter! } }
3. Interface with default methods vs abstract class
Even though we can add concrete methods in the form of default methods to the interface , interface are not equal to abstract classes. Following table illustrates Interface with default methods vs abstract class in java.
Interface with default Methods | Abstract Class |
---|---|
Inside interface every variable is always public static final and there is no chance of instance variables | Inside abstract class we can declare instance variables which Are required to the child class. |
Inside interface we can’t declare constructors. | Inside abstract class we can declare constructors. |
Inside interface we can’t declare instance and static blocks. | Inside abstract class we can declare instance and static blocks. |
Functional interface with default methods can refer lambda expression. | Abstract class can’t refer lambda expressions. |
Inside interface we can’t override Object class methods. | Inside abstract class we can override Object class methods. |
4. Conclusion
In this article we have covered java default method in interface with examples and and interface with default methods vs an abstract class. In addition to the default methods java 8 allows to define static methods in interfaces.