Implementing multiple interfaces in Java
A class can implement many interfaces.
One parent, many contracts
A class can extend only one class, but it can implement any number of interfaces, separated by commas. Its objects then count as every one of those types and can be passed wherever any of them is expected.
interface Flyer { void fly(); }
interface Swimmer { void swim(); }
class Duck implements Flyer, Swimmer {
public void fly() { }
public void swim() { }
}The word order is fixed
When a class has both, **extends comes first, then implements. Interfaces can also combine: an interface may extend several interfaces** at once.
class Robot extends Machine
implements Walker, Talker { }
interface Amphibian
extends Flyer, Swimmer { }How many types is a duck?
What does this print?
interface Flyer { }
interface Swimmer { }
class Duck implements Flyer, Swimmer { }
void main() {
Object o = new Duck();
System.out.println(o instanceof Swimmer);
System.out.println(o instanceof Comparable);
}true falsefalse falsetrue true
Show the answer
A Duck is a Flyer and a Swimmer — every interface its class implements, in any listed position. It never signed Comparable, so that check is false.
Backwards declaration
Writing implements before extends is a compile error, even though both parts are fine on their own. The legal order is class Kid extends Thread implements Runnable (legal, if redundant — Thread already implements Runnable).
class Kid implements Runnable
extends Thread { } // errorSame method, two contracts
Door and Window both declare the abstract method void close();. Class Car implements both. Does Car need two close() methods?
Think about it, then reveal the answer
No — **one public close() satisfies both**. Identical abstract signatures are the same requirement, and neither interface supplies a body, so nothing competes. (When two interfaces *do* bring bodies, things get spicier — soon!)
Real classes wear many badges
ArrayList implements List, RandomAccess, Cloneable and Serializable all at once. Your own domain classes do the same: an Order can be Comparable<Order> for sorting and Serializable for caching, while still extending your BaseEntity.
Key takeaways
- class Duck implements Flyer, Swimmer { ... }
- extends comes first: class A extends B implements C, D
- An interface may extend several interfaces: interface C extends A, B
- Same abstract method in two interfaces? One implementation satisfies both
💡 One person can hold a driver's licence, a library card and a passport at the same time.
Java's own String implements five interfaces: Serializable, Comparable, CharSequence, plus Constable and ConstantDesc, which were added in Java 12.
Practice questions
Robot already has a superclass. Which keyword fills the blank?
class Robot extends Machine ___ Walker, Talker { }- implements
- extends
- uses
- inherits
Check your answer
implements. Classes are extended once; interfaces are implemented, and you list them after implements separated by commas.
What does this print?
interface A { }
interface B { }
class C implements A, B { }
void main() {
Object o = new C();
System.out.println(o instanceof A);
System.out.println(o instanceof B);
System.out.println(o instanceof Runnable);
}- true true false
- true false false
- false false false
- Compile error
Check your answer
true true false. A C object is an A and a B because its class implements both. It isn't a Runnable, so that check is false.