Java 8 – Default Methods

December 17, 2018 Off By ravi24kant

In the previous post we’ve seen functional Interface & Lambda Expressions. during this post we are going to see another java 8 new feature “Default Methods” in interfaces. Before Java 8, the interface solely contains method signatures. With Java 8 new feature Default methods you’ll be able to include method body inside the interface.

Lets start with an example:
package com.ravivermajavablog.defaultM;

public interface MyFirstInterface {

default void dMethod(){
System.out.println("from MyFirstInterface...");
}
}

OK, what’s default here? this is the new approach of declaring the method body in Java 8 for AN interface. As seen above, default methods comes together with implementation. You dont got to implement default methods if your class implements above interface, below is an example:

package com.ravivermajavablog.defaultM;

public class TryDefaultMethod implements MyFirstInterface {
    public static void main(String[] args) {
    new TryDefaultMethod().dMethod();
   }
}

We can also override the default methods, but you dont need to specify default in the method signature.

package com.ravivermajavablog.defaultM;

public class TryDefaultMethod implements MyFirstInterface {
  @Override
  public void dMethod(){
    System.out.println("from TryDefaultMethod class...");
  }
}

This is not all done here.

Default Methods – Best part comes as following benefits:

  • Static default methods: you’ll be able to define static default methods in interface which can be offered to any or all instances of class that implement this interface. This makes it easier for you to arrange helper methods in your libraries; you’ll be able to keep static methods specific to an interface within the same interface instead of in a very separate class. this permits you to define methods out of your class and nonetheless share with all child classes.
  • They supply you an extremely desired capability of adding a capability to variety of classes without even touching their code. simply add a default method in interface that all of them implement.

Default Methods – Why do we need to implement a method inside the interface?

Let’s say you have an interface that has multiple methods, and multiple classes are implementing this interface. one of the method implementation is common across the class, we will build that method as a default method, so the implementation is common for all classes.

another Simplest answer is to enable the functionality of lambda expression in java. Lambda expression are basically of kind of functional interface. To support lambda expressions seamlessly, all core classes ought to be changed. however these core classes like java.util.List are implemented not only in JDK classes, however also in thousands of consumer code likewise. Any incompatible modification in core classes can back fireplace for sure and can not be accepted in the slightest degree.

where you’ve got already existing application, for a brand new requirements we’ve to add a method to the existing interface. If we add new method then we want to implement it through out the implementation classes. By using the Java 8 default method we will add a default implementation of that method that resolves the problem.

Default Methods -When working with multiple inheritance:

If we have two interfaces, one with default method and another with just method signature (normal way of defining method in the interfaces).

public interface MyFirstInterface {
 default void dMethod(){
   System.out.println("i am from MyFirstInterface");
  }
}
public interface MySecondInterface {
   void dMethod();
}

If any class implements two of the above interfaces, then dMethod() method became abstract method. We have to implement dMethod() method with in the implementation class.

If we have two interfaces, both are having same default method with different implementation, as shown below:

public interface MyFirstInterface {
   default void dMethod(){
   System.out.println("I Am from my first interface");
 }
}

public interface SecondInterface {
  default void dMethod(){
   System.out.println("Am from my second interface");
 }
}

If any class implements above two interfaces, there will be an ambiguity. So we have to implement dMethod() method.

If we want to call the perticular default method implementation then you can call like
<InterfaceName>.super.<method-name>() as shown below:
public class TryDefaultMethod implements MyFirstInterface,MySecondInterface {
 public void dMethod() {
  MyFirstInterface.super.dMethod();
 }
}

If we have two interfaces, both are having same default method with different implementation. But one interface extends other as shown below:

public interface MyFirstInterface {
  default void dMethod(){
  System.out.println("Am from first interface");
 }
}

public interface SecondInterface extends MyFirstInterface{
  default void dMethod(){
   System.out.println(" I am from second interface");
 }
}

Here SecondInterface overrides the dMethod() method from MyFirstInterface.

Now the big Question is, What is the difference between abstract class and interface?

Abstract class will have constructor, wherever you wish an object to call the methods in subclass. however just in case of default method without any reference you’ll invoke the interface method, like InterfaceName.super.method()

With this above discussion, i am sure you understand the use and importance of Default method.