Default Methods


Default Methods

In this tutorial, we will discuss default methods in Java 8. Have you ever faced a situation, when you created an interface and many classes implemented that interface? Now you need to add new methods to the interface. After adding new methods, your Java project will be full of compilation errors because you need to add these new methods to all classes which are implementing that interface (If a class implements an interface then you have to implement all its methods in the class)

Default Methods in java
  • Until the 1.7 version onwards inside the interface, we can take only public abstract methods and public static final variables (every method present inside the 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.
  • But from the 1.8 version onwards in addition to these, we can declare default concrete methods also inside the interface, which are also known as defender methods.
Why default methods?

The oneliner for this could be “backward compatibility”.If JDK modifies an interface, then all classes which implement this interface will break. By adding a default method in the interface, you can provide the default implementation of it without affecting implementing classes as it includes implementation of that method and any implementing class which needs that method can override it.

For adding lambda expression in Java 8, JDK needs to add methods(such as foreach) to the List or collections Interface, but if you add this method to this interface, it will break millions of lines of code as the class which implements the interface, need to implement all its methods.

We can declare a 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 the requirement implementation class can use these default methods directly or can override them.

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 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).

Note

We can’t override object class methods as default methods inside the 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 a default method with the same signature then there may be a chance of ambiguity problem (diamond problem) in the implementation class. To overcome this problem compulsory we should override the 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 the default method in the implementation class?

In the implementation class, we can provide a completely 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 an 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.

Capture 45

Hence

        Interface with default method != abstract class
Default Methods
Scroll to top