← Back to Core Java Course | Chapter 9: OOP Advanced | Lesson 5 of 8

Java Multiple Interfaces

Implementing Multiple Interfaces

While a Java class can extend only one parent class, it can implement as many interfaces as it needs simultaneously, separating each interface name with a comma after implements — this sidesteps the ambiguity problems that multiple class inheritance would create.

Example: Implementing Multiple Interfaces

java
interface Flyable {
	void fly();
}
interface Swimmable {
	void swim();
}
class Duck implements Flyable, Swimmable {
	public void fly() { System.out.println("Flying"); }
	public void swim() { System.out.println("Swimming"); }
}
public class Main {
	public static void main(String[] args) {
		Duck duck = new Duck();
		duck.fly();
		duck.swim();
	}
}

Interface Inheritance

Interfaces themselves can extend other interfaces using extends, letting you build layered contracts where a more specific interface inherits and adds to the requirements of a more general one.

Example: Interface Inheritance

java
interface Animal {
	void eat();
}
interface Pet extends Animal {
	void play();
}
class Dog implements Pet {
	public void eat() { System.out.println("Eating"); }
	public void play() { System.out.println("Playing"); }
}
public class Main {
	public static void main(String[] args) {
		new Dog().eat();
	}
}

Multiple Interface Inheritance

An interface can even extend several parent interfaces at once (interfaces don't have the single-inheritance restriction classes do), listing each parent interface name separated by commas, and the extending interface inherits all their abstract method requirements combined.

Example: Multiple Interface Inheritance

java
interface A {
	void methodA();
}
interface B {
	void methodB();
}
interface C extends A, B { // extends both A and B
}
class Impl implements C {
	public void methodA() { System.out.println("A"); }
	public void methodB() { System.out.println("B"); }
}
public class Main {
	public static void main(String[] args) {
		new Impl().methodA();
	}
}

Method Name Collisions

If a class implements two interfaces that happen to declare a method with the identical signature, the class only needs to write one implementation to satisfy both requirements at once — Java treats it as a single method that happens to fulfill two contracts simultaneously.

Example: Method Name Collisions

java
interface Flyable {
	void move();
}
interface Swimmable {
	void move();
}
class Duck implements Flyable, Swimmable {
	public void move() { // one implementation satisfies both
		System.out.println("Duck moves");
	}
}
public class Main {
	public static void main(String[] args) {
		new Duck().move();
	}
}

Solving Multiple Inheritance limits

Multiple interfaces sidestep the classic "diamond problem" that multiple class inheritance can cause, because interfaces (aside from default method bodies) don't carry actual field state — there's no ambiguous shared data to conflict over, only method contracts to satisfy.

Example: Solving Multiple Inheritance limits

java
interface Flyable {
	void fly();
}
interface Swimmable {
	void swim();
}
class Duck implements Flyable, Swimmable { // no diamond problem: no shared field state
	public void fly() { System.out.println("Flying"); }
	public void swim() { System.out.println("Swimming"); }
}
public class Main {
	public static void main(String[] args) {
		new Duck().fly();
	}
}
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.