Java Multiple Interfaces
In this page:
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: