Interface Inheritance in Java
intermediate· OOP · Interfaces
Problem
An interface can extend another interface, inheriting its method signatures and adding more of its own — a class implementing the child interface must supply methods from both.
Define a RefundablePayable interface that extends Payable, and implement it in a single class.
Input
amount = 100.0
Output
Paid: 100.0
Refunded: 100.0
Java Program
Java
interface Payable {
void pay(double amount);
}
interface RefundablePayable extends Payable {
void refund(double amount);
}
class Order implements RefundablePayable {
public void pay(double amount) {
System.out.println("Paid: " + amount);
}
public void refund(double amount) {
System.out.println("Refunded: " + amount);
}
}
public class InterfaceInheritanceDemo {
public static void main(String[] args) {
Order order = new Order();
order.pay(100.0);
order.refund(100.0);
}
}Output
Paid: 100.0
Refunded: 100.0
Core Logic
Extending one interface from another folds the parent's method signatures into the child, so a class implementing only the child ends up responsible for both.
How It Works
- 1
interface Payabledeclaresvoid pay(double amount);. - 2
interface RefundablePayable extends Payableaddsvoid refund(double amount);on top of the inheritedpay. - 3
class Order implements RefundablePayablemust supply bothpay()andrefund(), even though onlyrefund()is declared directly on the interface it names. - 4Calling either method on an
Orderobject runs the matching implementation.
Calling
order.pay(100.0) then order.refund(100.0) runs both implementations in turn, printing "Paid: 100.0" and "Refunded: 100.0".💡
Key Point: Unlike classes, interfaces can extend more than one other interface at once — a child interface can pull in method signatures from several parents simultaneously.
Key Concepts
interface extends interfaceimplementsinherited abstract methods