Default Methods

Default Methods
  • Until 1.7 version on wards inside interface we can take only public abstract methods and public static final variables (every method present inside interface is always public and abstract whether we are declaring or not).
  • Every variable declared inside interface is always public static final whether we are declaring or not.
  • But from 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.
  • We can declare default method with the keyword β€œdefault” as follows
default void m1(){ 
   System.out.println (β€œThis is Default Method”); 
}
  • interface default methods are by-default available to all implementation classes. Based on requirement implementation class can use these default methods directly or can override.
package com.ashok.java8;

/**
 * 
 * @author ashok.mariyala
 *
 */
interface Interf {
   default void m1(){ 
      System.out.println (β€œHello..!! This is Default Method”); 
   }
}

class Test implements Interf { 
   public static void main(String[] args) { 
      Test t = new Test(); 
      t.m1();
   } 
}
  • Default methods also known as defender methods or virtual extension methods.
  • The main advantage of default methods is without effecting implementation classes we can add new functionality to the interface (backward compatibility).

Note

We can’t override object class methods as default methods inside interface otherwise we get compile time error.

interface Interf {
   default inthashCode(){ 
      return 100; 
   }
}
CompileTimeError

Reason

Object class methods are by-default available to every Java class hence it’s not required to bring through default methods.

Default method vs multiple inheritance

Two interfaces can contain default method with same signature then there may be a chance of ambiguity problem (diamond problem) to the implementation class. To overcome this problem compulsory we should override default method in the implementation class otherwise we get compile time error.

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

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

class Test implements Left, Right {
   // Implementation
}
How to override default method in the implementation class?

In the implementation class we can provide complete new implementation or we can call any interface method as follows.

interfacename.super.m1();
class Test implements Left, Right { 
   public void m1() {
      System.out.println("Test Class Method"); OR Left.super.m1(); 
   }
   public static void main(String[] args) {
      Test t = new Test(); 
      t.m1();
   }
}

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 abstract class.

Hence

        Interface with default method != abstract class
Default Methods


Scroll to top