Monday, 4 May 2015

interface static method in Java 8

Java 8 has added support for interface default methods as well as interface static methods which is one of the important change in Java 8 along with the addition of lambda expressions and stream API. This post talks about the interface static methods in Java.

Static method in an interface

Like static methods in a class, now we can write static method in interfaces too. Static methods in an interface can be called independently of any object just like how static methods are called in a class.

General form of calling the interface static method

InterfaceName.staticMethodName;

Example Code

public interface MyInterface {
 int method1();
 // default method, providing default implementation
 default String displayGreeting(){
    return "Hello from MyInterface";
 }
 //static method
 static int getDefaultAmount(){
    return 0;
 }
}
public class MyClass{ 
 public static void main(String[] args) { 
     int num = MyInterface.getDefaultAmount();
     System.out.println("num " + num);
  
 } 
}

Output

num 0

In the example code interface MyInterface has one static method getDefaultAmount(). Also note that MyClass is not even implementing MyInterface still from MyClass using the call MyInterface.getDefaultAmount(); static method of the MyInterface can be called. This is because, no implementation or reference of the interface is required to call the static method.

Also note that static interface methods are not inherited by

  • Implementing classes
  • Extending interfaces

Here interface B is extending mentioned interface MyInterface, but it can not access the static method of interface MyInterface.

interface B extends MyInterface{ 
 default String displayGreeting(){
     B.getDefaultAmount(); // Compiler Error 
     return "Hello from MyInterface";
 }
 
}

Same way even if MyClass implements MyInterface still it can not access static method of MyInterface, either by using the class name or by using the object reference.

public class MyClass implements MyInterface{
 // provides implementation for the non-default method
 // of the interface
 @Override
 public int method1() {
     return 10;
 }
 //Overriding the default method of MyInterface
 public String displayGreeting(){
     return MyInterface.super.displayGreeting();
 }
 
 public static void main(String[] args) {
     MyInterface myInt = new MyClass();
  
     int num = MyInterface.getDefaultAmount();
     System.out.println("num " + num);
     MyClass.getDefaultAmount(); // Compiler error
     myInt.getDefaultAmount();// Compiler error
  
 } 
}

Hiding interface method

But the implementing class can hide the interface method by providing method with same signature in the implementing class.

Example code

public interface MyInterface {
  int method1();
  // default method, providing default implementation
  default String displayGreeting(){
     return "Hello from MyInterface";
  }
  static int getDefaultAmount(){
     return 0;
  }
}
Implementing class
public class MyClass implements MyInterface{
  // provides implementation for the non-default method
  // of the interface
  @Override
  public int method1() {
     return 10;
  }
  //Overriding the default method of MyInterface
  public String displayGreeting(){
     return MyInterface.super.displayGreeting();
  }
 
  public  static int getDefaultAmount(){
     return 5;
  }
 
  public static void main(String[] args) {
     MyInterface myInt = new MyClass();
  
     int num = MyInterface.getDefaultAmount();
     System.out.println("num - Interface " + num);
     System.out.println("num - Class " + MyClass.getDefaultAmount());
  
  } 
}

Output

num - Interface 0
num - Class 5

Here getDefaultAmount() method is provided in the MyClass class also which hides the interface static method.

Advantages of interface static methods

  • Interface static methods can be used for providing utility methods.
  • With Interface static methods we can secure an implementation by having it in static method as implementing classes can't override them. Though we can have a method with same signature in implementing class but that will hide the method.

That's all for this topic interface static methods in Java 8. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. interface in Java
  2. Marker interface in Java
  3. Difference between abstract class and interface
  4. interface default methods in Java 8
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

No comments:

Post a Comment