static method in interfaces – java 8

/, Java 8/static method in interfaces – java 8

static method in interfaces :

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.

Example1 :

interface static methods overriding :

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

Example 1:

Example 2:

main() in interfaces :

From 1.8 version on wards we can write main() method inside interface and hence we can run interface.

Leave A Comment