We know that interface provide methods without body or functionality but in abstract class we can provide default functionality of methods. So in interface we can't give method body and can't declare ant static method.
But in Java 8 now we can declare static method and can declare some default methods using "default " keyword.
So now there's virtually no difference between an interface and an abstract class other than multiple inheritance.
We can change default method without changing any of its implementation.
Here is the example:
public class Example implements MyInterface {
public static void main(String[] args) {
// Your code here....
}
@Override
public void calculate() {
System.out.println("Calculation goin on...");
}
}
interface MyInterface {
public void calculate();
default public void messages() {
System.out.println("Hi what's goin on ?");
}
}
Here it's not required to implement all methods of interface.
we can also declare some static method in interface now
1 Comment(s)