Wednesday, August 22, 2018

Java9 - learn Interface Private Static methods | java9 features tutorials

In this blog post, We are going to learn the java9 interface private methods and static methods with examples

Java9 -  Interface Improvements

java9 Interface private and static methods example
Java released many versions. I am going to describe how the interface is evolved from public modifier to private modifier and abstract methods only to non-abstract methods.

Interface declaration before java7 version examples

The interface has only constants and abstract methods
public static final variables i.e constants
public abstract methods - No implementation with method header.
public interface MyInterface {
    public String message = "Hello";
    public void getMessage();
}

In the above interface example, We have defined one constant and one abstract method. There is no implementation for methods. The method has only declarations ie called abstract. If you want to have a non-abstract method, Then we can choose Abstract methods.

java8 interface additions -default and static methods with an example

With Java8 version, interface introduced few features to it. Public Default method and public Static methods can be declared inside an interface.

In this version, we are able to write code using below features.


  • public static final variables
  • public abstract methods
  • public Default methods
  • public Static methods

public interface MyInterface {
 public String CONSTANT = "constant";
 public void abstractMethod();
 // default method example
 public default void defaultMethod() {
  System.out.println("defaultMethod");
 }
 // static method example
 public static void staticMethod() {
  System.out.println("staticMethod");
 }
}
public class TestInterface implements MyInterface{
 public static void main(String[] args) {
  TestInterface t=new TestInterface();
  t.abstractMethod();
  t.defaultMethod();
  MyInterface.staticMethod();
 }
 @Override
 public void abstractMethod() {
  System.out.println("abstractMethod");
 }
}
Output is
abstractMethod
defaultMethod
staticMethod
Default methods can be overridden, can be called using an instance of the implementation class.
Static methods can not be overridden, can be called using interface name.

Java9 - interface private and static methods example

With java9 version, You can add private methods and private static method to interfaces.
For example, You have written a common code in many default methods in the interface. the redundant code is not a good way of design. So we will move redundant code to private methods to allow reusability. hence Private methods allow reusing the code in an interface. Implementation classes will have access to this private methods and below features.

  • public static final variables
  • public abstract methods
  • public Default methods
  • public Static methods
  • private methods
  • private static methods

public interface MyInterface {
 public String CONSTANT = "constant";
 public void abstractMethod();
 // default method example
 public default void defaultMethod() {
  privateMethod();
  privateStaticMethod();
  System.out.println("defaultMethod");
 }
 // static method example
 public static void staticMethod() {
  System.out.println("staticMethod");
 }
 // private method example
 private void privateMethod() {
  System.out.println("privateMethod");
 }
 // private static method example
 private static void privateStaticMethod() {
  System.out.println("privateStaticMethod");
 }
}
public class TestInterface implements MyInterface{

 public static void main(String[] args) {
  TestInterface t=new TestInterface();
  t.abstractMethod();
  t.defaultMethod();
  MyInterface.staticMethod();
 }
 @Override
 public void abstractMethod() {
  System.out.println("abstractMethod");
  
 }

}

And the output of the above code execution is
abstractMethod
privateMethod
privateStaticMethod
defaultMethod
staticMethod

Private and static methods interface features


  • These methods have a private keyword to denote it as a private method
  • private methods include body inside curly braces
  • The abstract keyword is not applicable to this methods as No private and abstract keywords applied to methods as both functionalities are reverse.
  • Private method accessibility is within the interface where it is declared
  • private Static methods can access other static and non-static methods ie default methods. private methods can not be used in private static methods


Please be aware of the usage of these methods in interfaces. If properly not used, Compiler gives 'Exception in thread "main" java.lang.StackOverflowError'

Related article


EmoticonEmoticon