Interface Static Method in Java 8

With the release of Java 8, we can also define static method within an interface. A static method within an interface is explicitly defined with a static keyword and method body.

It works nearly the same as static methods defined in classes. A static method defined in Java 8 interface is not inherited in any classes that implement the interface.

Rules of define Static method in Java 8 Interface


There are the following static interface method rules that you need to be familiar with:

1. Like all methods defined within an interface, a static method is considered to be public and will not compile if declared as private or protected.

2. To call the static method, a reference to the name of the interface must be used.

3. Abstract keyword cannot be used with static methods within an interface.

4. We cannot use the default keyword for static method because a default method is an instance in nature.

5. We cannot override the static method in the implementation class because the static methods are not inherited by the subclass and must be accessed with a reference to the interface name.

Static Method Interface Example Programs


Let’s take an example program where we will declare a static method within an interface. Look at the source code to understand better.

Program code 1:

public interface Perimeter 
{ 
  void msg(); 
  static int peri(int l, int b)
  { 
     return 2*(l + b); 
  } 
} 
public class Rectangle implements Perimeter 
{ 
  public void msg()
  { 
     System.out.println("Perimeter of rectangle"); 
  } 
public static void main(String[] args) 
{ 
   Perimeter p = new Rectangle(); 
     p.msg(); 
   int perimeter = Perimeter.peri(20,30); 
   System.out.println(perimeter); 
  } 
}
Output: 
        Perimeter of rectangle 100

Let’s take another example program based on this concept.


Program code 2:

public interface A 
{	
// Default method definition.
   public default int m1() 
   {
      return 10;	 
   }
   int m2(); // Normal abstract method definition.

// Static method declaration within an interface.
   static int m3() 
   {
      return 30;
   }
}
public class B implements A 
{
// Overriding abstract method in the subclass.
   public int m2() 
   {
     return 20; 
   }
}
public class AB {
public static void main(String[] args) 
{
   B b = new B();
   System.out.println(b.m1()); // Calling default method.
   System.out.println(b.m2()); // Calling abstract method of implementation class.
   System.out.println(A.m3()); // Calling static method using name of interface.
 }
}

Output:
       10
       20
       30

Explanation:

1. As you observe in this example, the method m3() works just like a static method as defined in a class. In other words, it can be accessed without an instance of the class using the A.m3() syntax.

2. Note that the compiler will automatically insert the access modifier public because all methods in an interface are considered to be public.

3. If we try to call static method without an explicit reference to the name of the interface, the code will not compile.

Program code 3:

public interface A 
{
// Default method.	
   public default void m1() 
   {
      System.out.println("Default method defined within an interface"); 
   }
  void m2(); // Normal abstract method definition.

// Static method declaration within an interface.
   static void m3() 
   {
     System.out.println("Static method defined within an interface");
   }
}
public class B implements A
{
// Overriding abstract method in the subclass.
   public void m2() 
   {
      System.out.println("Implementation of abstract method");	 
   }
   static void m3()
   {
      System.out.println("Static method defined within implementation class");	 
   }
}
public class AB {
public static void main(String[] args) 
{
   B b = new B();
   b.m1();
   b.m2();
   A.m3();
   B.m3();
  }
}
Output:
      Default method defined within an interface
      Implementation of abstract method
      Static method defined within an interface
      Static method defined within implementation class

Explanation:

As you can observe in this example, the static method cannot be inherited in the derived class and can be accessed with a reference to the name of interface.

Program code 4:

public interface A {	
 static void m1()
 {
   System.out.println("Static method in A");
 }
}
public interface B {
 static void m1()
 {
    System.out.println("Static method in B ");	 
 }
}
public class AB implements A, B{
public static void main(String[] args) 
{
   A.m1(); // Calling static method of interface A.
   B.m1(); // Calling static method of interface B.
 }
}
Output:
       Static method in A
       Static method in B

In this tutorial, we have explained all the important rules of defining a static method in Java 8 interface with the help of some example programs. Hope that you will have understood the basic concept of of Java 8 static interface method. In the next, we will understand polymorphism in Java with examples.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love