An interface in Java is a blueprint of a class that defines a set of abstract methods. It supports multiple inheritance and helps achieve 100% abstraction.
implements to inherit an interfacepublic and abstract by defaultAn interface defines what a class must do, not how it does it. A class implementing an interface must provide implementations for all methods.
// Interface declaration
interface Animal {
void sound();
}
// Class implementing interface
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
When the sound() method is called using a Dog object, the output will be:
// Program output
Dog barks
See how the interface Animal acts as a contract. Click buttons to switch implementation!
Vehicle with method move()Car and Bike classes