Multiple Interfaces in Java
Problem
A Java class can implement more than one interface at once, even though it can only extend one class — each interface just adds its own set of methods the class must supply.
Define Payable and Printable interfaces, and implement both in a single Invoice class.
Java Program
interface Payable {
void pay(double amount);
}
interface Printable {
void print(double amount);
}
class Invoice implements Payable, Printable {
public void pay(double amount) {
System.out.println("Paid: " + amount);
}
public void print(double amount) {
System.out.println("Printing invoice for amount: " + amount);
}
}
public class MultipleInterfacesDemo {
public static void main(String[] args) {
Invoice invoice = new Invoice();
invoice.pay(250.0);
invoice.print(250.0);
}
}Output
Core Logic
Listing both interfaces after implements, separated by a comma, commits a single class to satisfying two independent contracts at once.
- 1
interface Payabledeclaresvoid pay(double amount);. - 2
interface Printabledeclaresvoid print(double amount);, a completely separate contract. - 3
class Invoice implements Payable, Printablecommits to supplying both methods. - 4Both methods can be called on the same
Invoiceobject, since it fully satisfies each interface.
invoice.pay(250.0) and invoice.print(250.0) in turn runs each interface's own implementation, defined in the same class.Key Point: Java classes can only extend one superclass but can implement any number of interfaces — this is how Java gets some of the flexibility of multiple inheritance without its ambiguity problems.
Key Concepts
Approach 2: Java 8
interface Payable {
void pay(double amount);
}
interface Printable {
void print(double amount);
}
public class MultipleInterfacesDemoLambda {
public static void main(String[] args) {
// Each lambda implements exactly one interface — no single lambda can implement both
Payable payable = amount -> System.out.println("Paid: " + amount);
Printable printable = amount -> System.out.println("Printing invoice for amount: " + amount);
payable.pay(250.0);
printable.print(250.0);
}
}
Output
Core Logic
Since both Payable and Printable have exactly one abstract method each, a lambda can implement either one directly — but never both at once the way a single class can.
- 1
PayableandPrintableare left unchanged — each still declares just one method. - 2
Payable payable = amount -> System.out.println("Paid: " + amount);assigns a lambda that implements onlyPayable. - 3
Printable printable = amount -> System.out.println("Printing invoice for amount: " + amount);assigns a second, separate lambda that implements onlyPrintable. - 4Two lambda variables are needed here, one per interface, since a single lambda expression can only ever implement one functional interface.
payable.pay(250.0) and printable.print(250.0) in turn produces the same two lines the single Invoice class did.Key Point: This is exactly where the class-based approach and lambdas diverge: Invoice implements Payable, Printable lets one object satisfy both contracts, while lambdas can only ever stand in for a single functional interface at a time — combining two would need two separate lambda objects, as shown here.