Java ProgramsOOPMultiple Interfaces

Multiple Interfaces in Java

beginner·  OOP  ·  Interfaces

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.

Input
amount = 250.0
Output
Paid: 250.0 Printing invoice for amount: 250.0

Java Program

Java
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

Paid: 250.0 Printing invoice for amount: 250.0

Core Logic

Listing both interfaces after implements, separated by a comma, commits a single class to satisfying two independent contracts at once.

How It Works
  1. 1interface Payable declares void pay(double amount);.
  2. 2interface Printable declares void print(double amount);, a completely separate contract.
  3. 3class Invoice implements Payable, Printable commits to supplying both methods.
  4. 4Both methods can be called on the same Invoice object, since it fully satisfies each interface.
Calling 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

multiple interfacesimplementsinterface contract

Approach 2: Java 8

Java
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

Paid: 250.0 Printing invoice for amount: 250.0

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.

How It Works
  1. 1Payable and Printable are left unchanged — each still declares just one method.
  2. 2Payable payable = amount -> System.out.println("Paid: " + amount); assigns a lambda that implements only Payable.
  3. 3Printable printable = amount -> System.out.println("Printing invoice for amount: " + amount); assigns a second, separate lambda that implements only Printable.
  4. 4Two lambda variables are needed here, one per interface, since a single lambda expression can only ever implement one functional interface.
Calling 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.

Key Concepts

functional interfacelambda expression

Related Programs