1) static method in interface in java 8
Now, we can write static method in interface in java 8. (Before java 8, we could only write abstract method in interfaces in java 8)
We can define these static methods in interface, just like normal static method.
3) Full Program/Example to create and use static method of interface in Java 8 >
/*
Full Example to use static method of interface in Java 8
*/
interface Animals {
/*
* Create/Define static method in interface
*/
public static void food() {
System.out.println("Animal eat food");
}
}
public class MainClass{
public static void main(String...args) {
//Call static method of interface
Animals.food();
}
}
/* OUTPUT
Animal eat food
*/
|
First, we defined static method in interface.
Then we used it in main method in MainClass.
4) Can we override static method of interface in Java 8?
No, we cannot override static method of interface in subclass in java 8.
Let’s see program -
First, we defined static method in interface, we cannot override it in subclass.
Then, we saw attempt to override static method of interface produced compilation error in java 8.
5) Advantage of static method in interface in java 8?
We can make utility interface which consists of static methods in java 8.
For example -
java.util.Collections is a utility class which consists of static methods that operate on or return Collection in java.
6) Facts about static method in java 8 >
- static method are public by default in java 8.
Also read >