Interfaces changes in Java 8 & Java 9

Before java 8 an interface can have only constant variables and abstract methods. Java 8 and 9 introduced some new features in interfaces like default methods, static methods and private methods. Let's see these features of interfaces one by one.

Interface changes in Java 8

Java 8 has introduced two new features in interfaces, default methods and static methods. From java 8 onward an interface can have following components inside it.

  • Constant variables
  • Abstract methods
  • Nested Types
  • Default methods
  • Static methods

Default methods in java interfaces

From java 8 onward an interface can have default method with a body. A default method is defined using default keyword. The syntax of declaring a default method is :

 interface interfaceName {
    default return_type methodName() {
      // method definition
    }
  }
  
 Example: 
  
 interface MyInterface {
    default void print() {
      // method definition
    }
  }                 

Some key points about default methods are :

  • A default method must have a body.
  • The access modifier of default methods are implicitly public.
  • The class implementing such interface are not required to implement default methods. If needed, implementing class can override default methods.

Default methods enable you to add new functionality to the interfaces of your application and ensures that your old version of code won't break as it doesn't force the classes to implement this. Before java 8 adding new method in an interface broke the classes which already had implemented that interface.

The purpose of providing this feature is to allow the developers to add new functionalities to their interfaces whenever needed, without breaking their older version of code.

Java program of default methods in interface

 interface MyInterface  { 
    void calculateArea();       
    default void print() {
      System.out.println("This is default method");
    }
  }
  
 class DefaultMethod implements MyInterface {
    public void calculateArea() {
       System.out.println("Calculate area in this method");
     }
    public static void main(String args []) {
       DefaultMethod obj = new DefaultMethod();
       obj.print();
       obj.calculateArea();
     }
 }        

Output:

This is default method
Calculate area in this method

Static methods in java interfaces

From java 8 onward you can define static methods in an interface which can be called without the object of implementing class. The syntax of declaring static method is :

 interface interfaceName {
    static return_type methodName() {
      // method definition
    }
  }
  
 Example: 
  
 interface MyInterface {
    static void print() {
       // method definition
    }
  }                 

Some key points about static methods are :

  • A static method must have a body.
  • The access modifier of static methods are implicitly public.
  • These method must be called using interface name.
  • Since these methods are static, we cannot override them in implementing class.

Java program of static method in interface

 interface MyInterface  { 
    void calculateArea();       
    static void print() {
       System.out.println("This is static method");
     }
  }
  
 class StaticMethod implements MyInterface {
     public void calculateArea() {
       System.out.println("Calculate area in this method");
     }
    public static void main(String args []) {
       StaticMethod obj = new StaticMethod();      
       obj.calculateArea();
       MyInterface.print();
     }
 }        

Output:

Calculate area in this method
This is static method

Interfaces changes in Java 9

Java 9 introduced private methods and private static methods in interfaces. Static methods in java 8 can not be private but from java 9 it can be. From java 9 onward an interface can have following components inside it.

  • Constant variables
  • Abstract methods
  • Nested Types
  • Default methods
  • Static methods
  • Private methods
  • Private Static methods

Private and Private Static Methods in Java Interfaces

A private method is defined using private access modifier. The basic syntax of declaring a private and private static method in an interface is :

 interface interfaceName {
    private return_type methodName() {
       // method definition
     }      
    private static return_type methodName() {
       // method definition
     }
  }
  
 Example: 
  
 interface MyInterface {
    private void message() {
       // method definition
     }
    private static void detail() {
       // method definition
     }
  }                 

Some key points about private methods are :

  • A private method must have a body.
  • These method can be accessed within the interface only.
  • Since these methods are private, we cannot override them in implementing class.

The purpose of adding private methods in interfaces is to improve the code re-usability. These methods are generally used as utility methods which helps other defined methods inside that interface.

Java program of private method in interface

 interface MyInterface  {     
    default void print() {
       System.out.println("This is default method");
       // calling private methods
       message();
       detail();
     }
    private void message() {
      System.out.println("This is private method");
     }
    private static void detail() {
      System.out.println("This is private static method");
     }
  }
  
 class PrivateMethods implements MyInterface {
    public static void main(String args []) {
       PrivateMethods obj = new PrivateMethods();
       obj.print();
     }
  }        

Output:

This is default method
This is private method
This is private static method

★★★
  • An interface can have multiple default methods from java 8 onward.
  • An interface can have multiple private and static methods.
  • If an interface extends another interface, private methods are not inherited.
  • From java 8 onward we can define main method inside an interface and we can run it like classes.