Static method in Interface in Java

JavaServer Side ProgrammingProgramming

To implement static method in Interface, the Java code is as follows −

Example

 Live Demo

interface my_interface{
   static void static_fun(){
      System.out.println("In the newly created static method");
   }
   void method_override(String str);
}
public class Demo_interface implements my_interface{
   public static void main(String[] args){
      Demo_interface demo_inter = new Demo_interface();
      my_interface.static_fun();
      demo_inter.method_override("In the override method");
   }
   @Override
   public void method_override(String str){
      System.out.println(str);
   }
}

Output

In the newly created static method
In the override method

An interface is defined, inside which a static function is defined. Another function named ‘method_override’ is defined without a body. This interface is implemented by another class named ‘Demo_interface’. Inside this class, the main function is defined, and an instance of this ‘Demo_interface’ is also created. The static function is called on this instance, and next, the ‘method_override’ function is called on this instance. An override specification is written, under which the ‘method_override’ is defined. This function just prints the string on the console.

raja
Published on 13-Jul-2020 16:00:29
Advertisements