Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.
All the methods in an interface must be abstract, you cannot have a concrete method (the one which has body) if you try to do so, it gives you a compile time error saying “interface abstract methods cannot have body”.
In the following Java program, we are trying to write a method with a body (concrete) in an interface.
public interface MyInterface{ public static final int num =40; public void demo(){ System.out.println("This is a concrete method in the interface"); } }
On compiling, this program generates the following compile time error.
MyInterface.java:3: error: interface abstract methods cannot have body public void demo(){ ^ 1 error