Search This Blog

Monday 6 February 2017

Default Methods in Java 8

Default Methods in Java 8
With the release of Java 8, it is now possible for an interface method to define a default implementation. This new capability is called the default method.

Default method enables us a means by which interfaces could be expanded without breaking pre-existing code.
In simple terms default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface.


When a non-abstract class implements an interface, it must implement all methods defined by that interface. If a new method is to an existing interface, then the addition of that method would break pre-existing code, because no implementation would be found for that new method in pre-existing classes. The default method solves this problem by supplying an implementation that will be used if no other implementation is explicitly provided. Thus, the addition of a default method will not cause pre-existing code to break. This enables interfaces to be gracefully evolved over time without negative consequences.

Example of Default Method


public interface Account {
default void OpenAccount(){
      System.out.println("This is the Account Interface . . . .");
}
}



public class SavingAccount implements Account{
public void OpenSavingAccount(){
      System.out.println("This is the Saving Account Class . . . .");
}
}


public class Main {
public static void main(String[] args) {
      SavingAccount sa=new SavingAccount();
      sa.OpenAccount(); // Default method of interface is called
      sa.OpenSavingAccount();
}
}


Upon executing the Main class, we get the following output.
This is the Account Interface . . . .
This is the Saving Account Class . . . .

Default Methods and Multiple Inheritance
In case of multiple Inheritance, where both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.
interface InterfaceOne
{
      // Default method
      default void show()
      {
            System.out.println("Default InterfaceOne");
      }
}

interface InterfaceTwo
{
      // Default method
      default void show()
      {
            System.out.println("Default InterfaceTwo");
      }
}

public class MainClass implements InterfaceOne, InterfaceTwo
{
      // Overriding default show method
      public void show()
      {
            // use super keyword to call the show
            // method of InterfaceOne interface
            InterfaceOne.super.show();

            // use super keyword to call the show
            // method of InterfaceTwo interface
            InterfaceTwo.super.show();
      }

      public static void main(String args[])
      {
            MainClass d = new MainClass();
            d.show();
      }
}

Upon executing the MainClass, we get the following output.
Default InterfaceOne
Default InterfaceTwo


Important Points:
1.     Interfaces can have default methods with implementation from java 8 onwards.
2.     Interfaces can have static methods as well similar to static method of classes.
3.     Default methods were introduced to provide backward comparability for old interfaces so that they can have new methods without effecting existing code.


No comments:

Post a Comment