1. Introduction
Before Java 8, interfaces could have only abstract
methods. Java 8 has introduced default
method which allows interfaces to have methods with implementation. default
methods are declared with default keyword at the beginning of the method signature.
public interface MyInterface { // a regular interface method public abstract void play(); // a default method default void defaultMethod() { System.out.println("this is a default method"); } }
2. Need for default methods in Java interface
Consider a requirement when you want to add a method in an interface. Prior to Java 8, if you add a method in an interface then all classes implementing that interface should provide implementation of that method. So prior to Java 8, there was no way to add a method to an interface without breaking implementing classes.
To fix this issue, the concept of default
method was introduced in Java 8. default
method of an interface is by default available to all classes implementing the interface. So, default
methods enable you to add new functionality to your interface ensuring compatibility with older version of the interface.
3. Important points about default methods in Java interface
default
methods are allowed only in interfaces.default
methods are by defaultpublic
.default
methods can not beprivate
.default
methods are by default available to implementing classes.- A class implementing an interface having
default
method can override thedefault
method defined in interface.
4. Example of default method in Java interface
In the following example, MyInterface has a default method defaultMethod(). Since MyClass implements interface MyInterface, defaultMethod() is by default available in MyClass.
interface MyInterface { //a regular interface method public abstract void play(); //a default method default void defaultMethod() { System.out.println("this is a default method"); } } class MyClass implements MyInterface{ @Override public void play() { System.out.println("play method implementation"); } } public class Test { public static void main(String args[]) { MyClass obj = new MyClass(); obj.play(); obj.defaultMethod(); } }
Output
play method implementation
this is a default method
4.1 Overriding default method in class
Let us now try to override defaultMethod() in implementing class.
class MyClass implements MyInterface{ @Override public void play() { System.out.println("play method implementation"); } public void defaultMethod(){ System.out.println("Override the default implementation"); } }
When you run the code after overriding the defaultMethod(), following will be the output:
play method implementation
Override the default implementation
5. Extending interface that contain default methods
We know that an interface can extend another interface. For the sake of understanding, let us say these interfaces as parent interface and child interface, where parent interface contains a default method. In this case we can do the following:
1. Child interface may decide not to declare the default method at all. In this case, child interface will inherit the default method from parent.
interface ParentInterface { // a regular interface method public abstract void play(); // a default method default void defaultMethod() { System.out.println("this is parent interface default method"); } } interface ChildInterface extends ParentInterface { /* * If child does not mention default method at all child interface will inherit * default method */ }
2. Child interface can re-declare (but not redefine) the default method. In this case, default method in child interface will be abstract
.
interface ParentInterface { // a regular interface method public abstract void play(); // a default method default void defaultMethod() { System.out.println("this is parent interface default method"); } } interface ChildInterface extends ParentInterface { // Child interface declares default method which makes this as an abstract method void defaultMethod(); }
3. Child interface can redefine the default method. This will result in overriding of the parent’s default method in child interface.
interface ParentInterface { // a regular interface method public abstract void play(); // a default method default void defaultMethod() { System.out.println("this is parent interface default method"); } } interface ChildInterface extends ParentInterface { // Child interface can re-define default method default void defaultMethod() { System.out.println("this is child interface default method"); } }
6. Conclusion
Introduced in Java 8, default method allow you to define method implementation in interface. In this tutorial, we discussed the need of default methods and how to define default methods. We also discussed other scenarios related to default methods. Hope this article was helpful.