Home core java Java interface static method with examples

Java interface static method with examples

Java interface static method : Since Java 8, in addition to default methods we can define and implement static methods interfaces. In this article we will see about Java interface static methods and rules to define them in interfaces with examples.

1. Rules to define Java interface static method

1. From java 1.8 on wards in addition to default methods, you can define static methods in interfaces.

2. Interface static methods by default not available to the implementation classes, hence by using implementation class reference we can’t call interface static methods. We should call interface static methods by using interface name.

3. Like static methods in classes, you specify that a method definition in an interface is a static method with the static keyword at the beginning of the method signature.

4. All method declarations in an interface, including static methods, are implicitly public, so you can omit the public modifier.

2. Implementing static method in interface example

- Advertisement -
interface GreetInterface {
    static void wish(String user) {
       System.out.println("Hello "+user+"!");
      }
   }
 

public class StaticMethodTest1 implements GreetInterface {
 
     public static void main(String[] args) {
 
          StaticMethodTest1 smt = new StaticMethodTest1();
          //smt.wish("Peter"); // Compiler Error
          //StaticMethodTest1.wish("Peter");// Compiler Error
           GreetInterface.wish("Peter");// Hello Peter!
 }
}

2. Overriding Java interface static method :

1. As interface static methods by default not available to the implementation class, overriding concept is not applicable to static methods in interfaces. But we can define exactly same method in the implementation class, it’s valid but it is not overriding it is method hiding in child implementation classes.

interface GreetInterface {
    
     static void wish(String user) {
         
        System.out.println("Hello "+user+"!");
    }
}

2.1. Example 1:

public class StaticMethodTest2 implements GreetInterface {
    
    static void wish(String user) {
         
        System.out.println("Welcome "+user+"!");
    }
 
    public static void main(String[] args) {
        
        StaticMethodTest2 smt = new StaticMethodTest2);
        smt.wish("Peter"); // Welcome peter!
        
        GreetInterface gi = new StaticMethodTest1();
        //gi.wish("Peter"); // Compile time error, can only access to GreetInterface.wish         
        GreetInterface.wish("Peter");// Hello Peter!
    }
}

2.2. Example 2 :

public class StaticMethodTest2 implements GreetInterface {
    
    public void wish(String user) {
         
        System.out.println("Welcome "+user+"!");
    }
 
    public static void main(String[] args) {
        
        StaticMethodTest2 smt = new StaticMethodTest2();
        smt.wish("Peter");// Welcome Peter! 
        
        GreetInterface gi = new StaticMethodTest1();
        //gi.wish("Peter"); // Compile time error, can only access to GreetInterface.wish         
        GreetInterface.wish("Peter");// Hello Peter!
    } 
}

3. main() in interfaces :

From 1.8 version on wards we can write main() method inside interface and hence we can run interface. Before Java 8 version it’s not possible to write main method in interfaces as it is a static method.

interface GreetInterface {
    
     static void wish(String user) {
        System.out.println("Hello "+user+"!");
    }
     
     public static void main(String[] args) {
        wish("Peter");// Hello Peter!
    }
}

4. What is the use of static method in interface Java 8?

We can use static methods in interface to provide utility methods, instead of maintaining related functionality in different classes, interface static methods allow us to maintain them within interface itself and we can access them without having instance of it.

References

  1. Java documentation
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles