Default Methods In Interface In Java

Default Methods In Interface In Java | From Java 1.8 version onwards, we can also define default methods in an interface. Let us see what it means, how to define default methods in an interface, how to use them, and more. Also see: Java 8: Static Methods in Interface

  • Until Java 1.7 version, An interface can contain only public abstract methods and public static final variables. Till Java 1.7 version, every method present inside an interface is always public and abstract whether we are declaring or not.
  • Every variable declared inside the interface is always public static final whether we are declaring or not.
  • From Java 1.8 version onwards in addition to these, we can also declare default concrete methods inside the interface, which are also known as defender methods.

We can declare a default method with the keyword “default” as follows. Note that this “default” isn’t representing the modifier:-

interface intf {
   default void m1(){
      System.out.println ("Default Method");
   }
}

Default methods declared in Interface are by default available to all implementation classes. Based on the requirement, the implementation class can use these default methods directly or can override them.

interface Interf {
   default void m1(){
      System.out.println("Default Method");
   }
}
public class Test implements Interf {
   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
   }
}

Output:-

Default Method

Default methods are also known as defender methods or virtual extension methods. The main advantage of default methods is without affecting implementation classes we can add new functionality to the interface (backward compatibility).

interface interf {
   default void m1(){
      System.out.println("Default Method");
   }
}
public class Test implements Interf {
   @Override
   public void m1() {
      System.out.println("Test class m1()");
   }
   public static void main(String[] args) {
      Test t = new Test();
      t.m1();
            
      Interf i2 = new Test();
      i2.m1();
   }
}

Output:-

Test class m1()
Test class m1()

Note: We can’t override object class methods as default methods inside the interface otherwise we will get compile time error. Reason: Object class methods are by-default available to every Java class hence it’s not required to bring through default methods.

interface Interf {
   default int hashCode() {
      return 10;
   }
}

Compile-time error:-

A default method cannot override a method from java.lang.Object.

Default Methods in Multiple Inheritance

Two interfaces can contain a default method with the same signature. In that case, there may be a chance of ambiguity problem (diamond problem) in the implementation class which is implementing both interfaces. To overcome this problem, we must override the default method in the implementation class otherwise we get compile time error.

In the implementation class, we can provide a completely new implementation or we can call any interface method as follows:- interface-name.super.methodName();

interface A {
    default void m1() {
        System.out.println("A Default Method");
    }
}

interface B {
    default void m1() {
        System.out.println("B Default Method");
    }
}

class Test implements A, B {
    public void m1() {
        System.out.println("Test Class Method");
        // Or
        // A.super.m1();
        // Or,
        // B.super.m1();
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.m1();

        // We can't use them here
        // A.super.m1(); // Compile-time Error
        // B.super.m1(); // Compile-time Error
    }
}

Differences Between Interface with Default Methods and Abstract Class

Even though we can add concrete methods in the form of default methods to the interface, it won’t be equal to an abstract class. Interface with the default method is not exactly similar to an abstract class.

Interface with Default MethodsAbstract Class
Interface never talks about the state of the Object because it doesn’t contain any instance variable.Inside the abstract class, there may be a chance of instance variables that are required for the child class.
Interface never talks about the state of the Object because it doesn’t contain any instance variable.An abstract class can talk about the state of an Object because it can contain instance variables.
Inside the interface, we can’t declare Constructors.Inside the abstract class, we can declare Constructors.
Inside the interface, we can’t declare Instance and static blocks.Inside the abstract class, we can declare Instance and static blocks.
Functional interface with default methods Can refer to lambda expression.Abstract class can’t refer to lambda Expressions.
Inside the interface, we can’t override Object class methods.Inside the abstract class, we can override Object class methods.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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