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

Java Multiple Interfaces

A class can sign up to several interfaces at once, like a person who is both a student and a swimmer. It must then do everything each one requires.
Syntax
java
class ClassName implements InterfaceA, InterfaceB {
    // implement all methods of both 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
// Define the interface Flyable
interface Flyable {
	void fly();
}
// Define the interface Swimmable
interface Swimmable {
	void swim();
}
// Define the class Duck
class Duck implements Flyable, Swimmable {
	public void fly() { System.out.println("Flying"); }
	public void swim() { System.out.println("Swimming"); }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Duck object and store it in duck
		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
// Define the interface Animal
interface Animal {
	void eat();
}
// Define the interface Pet
interface Pet extends Animal {
	void play();
}
// Define the class Dog
class Dog implements Pet {
	public void eat() { System.out.println("Eating"); }
	public void play() { System.out.println("Playing"); }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	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();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Two interfaces with the same default method, which is a compile error unless the class overrides it.
  2. Using extends for a class with interfaces, when a class uses implements.
  3. Forgetting a method from one of the interfaces.
🔒

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.