Static Method in Interfaces
The static methods in interface are similar to default method.
It defined in the interface with the keyword static.
These methods cannot be overridden or changed in the implementation class.
We must need to implements the static method in the implementation classes same as default-method.
They are declared using the static keyword and will be loaded into the memory along with the interface.
If your interface has a static method you need to call it using the name of the interface, just like static methods of a class.
You cannot override the static method of the interface.
Syntax
public interface Test
{
void normalMethod();//normal interface methods
static void staticMethod()
{
// static method implementation
}
}
Program:-
interface Test
{
default void m1()
{
System.out.println("Default Method...");
}
static void m2()
{
System.out.println("Static Method...");
}
void normalInterfaceMethod(String str);
}
public class Child implements Test
{
public void normalInterfaceMethod(String str)// implementing abstract method
{
System.out.println(str);
}
public static void main(String[] args)
{
Child obj = new Child();
obj.m1();//calling the default method of interface
Test.m2();//calling the static method of interface
obj.normalInterfaceMethod("Normal Abstract Method..."); //calling the abstract method of interface
}
}
OutPut:-
Default Method...
Static Method...
Normal Abstract Method...