About Lesson
Interface inheritance allows one interface to inherit the methods and constants of another interface. This mechanism enables the creation of a hierarchy of interfaces, where a subclass interface inherits the behavior of its superclass interface.
- Superinterface: An interface that is extended by another interface is referred to as a superinterface.
- Subinterface: An interface that extends another interface is referred to as a subinterface.
- Extending Syntax: Use the
extends
keyword to indicate interface inheritance.
Implementing Interfaces with Inheritance
Classes implementing a subinterface must provide implementations for all methods defined in both the subinterface and its superinterfaces.
// Base interface
public interface Movable {
void move();
}
// Derived interface
public interface Flyable extends Movable {
void fly();
}
// Implementing the derived interface
public class Airplane implements Flyable {
@Override
public void move() {
System.out.println("Airplane is moving on the runway.");
}
@Override
public void fly() {
System.out.println("Airplane is flying.");
}
}
public class Main {
public static void main(String[] args) {
Airplane airplane = new Airplane();
airplane.move(); // Output: Airplane is moving on the runway.
airplane.fly(); // Output: Airplane is flying.
}
}