Monday, 4 May 2015

interface static method in Java 8

Java 8 has added support for default methods as well as static methods to interfaces. Here we'll talk about interface static methods.

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

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

Output

num 0

It can be noted that MyInterface interface has one static method getDefaultAmount(). Also note that MyClass is not even implementing MyInterface still from MyClass using the MyInterface.getDefaultAmount(); call static method 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

  • Imlementing 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 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
  
 } 
}

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 default 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