a class implements an interface using implements keyword, agreeing to provide implementations for all abstract methods defined in the interface.
rules -
a class must implement all abstract methods of an interface, or it must be declared abstract.
a class can implement multiple interfaces.
the implementing class must use public access for interface methods (since interfaces methods are implicitly public)
interfaces support polymorphism, an interface reference can point to any implementing class’s object.
interface Movable {
void move(); // Abstract method
default void stop() {
System.out.println("Stopping...");
}
}
class Car implements Movable {
@Override
public void move() {
System.out.println("Car is moving.");
}
}
class Bike implements Movable {
@Override
public void move() {
System.out.println("Bike is moving.");
}
}
class Main {
public static void main(String[] args) {
Movable car = new Car();
Movable bike = new Bike();
car.move(); // Output: Car is moving.
car.stop(); // Output: Stopping...
bike.move(); // Output: Bike is moving.
bike.stop(); // Output: Stopping...
}
}