Tutorialspoint.dev

Static method in Interface in Java

Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods cannot be overridden or changed in the implementation class.

Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but these methods cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.

Below programs illustrate static methods in interfaces:

Program 1: To demonstrate use of Static method in Interface.

In this program, a simple static method is defined and declared in an interface which is being called in the main() method of the Implementation Class InterfaceDemo. Unlike the default method, the static method defines in Interface hello(), cannot be overridden in implementing the class.



// Java program to demonstrate
// static method in Interface.
  
interface NewInterface {
  
    // static method
    static void hello()
    {
        System.out.println("Hello, New Static Method Here");
    }
  
    // Public and abstract method of Interface
    void overrideMethod(String str);
}
  
// Implementation Class
public class InterfaceDemo implements NewInterface {
  
    public static void main(String[] args)
    {
        InterfaceDemo interfaceDemo = new InterfaceDemo();
  
        // Calling the static method of interface
        NewInterface.hello();
  
        // Calling the abstract method of interface
        interfaceDemo.overrideMethod("Hello, Override Method here");
    }
  
    // Implementing interface method
  
    @Override
    public void overrideMethod(String str)
    {
        System.out.println(str);
    }
}

Output:

Hello, New Static Method Here
Hello, Override Method here

Program 2: To demonstrate Scope of Static method.

In this program, the scope of the static method definition is within the interface only. If same name method is implemented in the implementation class then that method becomes a static member of that respective class.

// Java program to demonstrate scope
// of static method in Interface.
  
interface PrintDemo {
  
    // Static Method
    static void hello()
    {
        System.out.println("Called from Interface PrintDemo");
    }
}
  
public class InterfaceDemo implements PrintDemo {
  
    public static void main(String[] args)
    {
  
        // Call Interface method as Interface
        // name is preceeding with method
        PrintDemo.hello();
  
        // Call Class static method
        hello();
    }
  
    // Class Static method is defined
    static void hello()
    {
        System.out.println("Called from Class");
    }
}

Output:

Called from Interface PrintDemo
Called from Class


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter