Home core java Java default method in interface or method extension

Java default method in interface or method extension

- Advertisement -

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.

1. default method in interface

  1. Until 1.7 version inside interface we can declare only public abstract methods and public static final variables, concrete methods are not allowed.
  2. 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.
  3. Default methods are allowed only in interfaces.
  4. All method declarations in an interface, including default methods, are implicitly public, so you can omit the public 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 MethodsAbstract Class
Inside interface every variable is always public static final and there is no chance of instance variablesInside 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.

References

  1. Java documentation
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories