Learn Java
import com.learnjava.oopadvanced.PolymorphismInterfaces;

Polymorphism Through Interfaces in Java

@Concept

Just like a superclass-typed variable can refer to any of its subclasses' objects, a variable typed as an interface can refer to an object of any class that implements it — Flyable f = new Bird(); is legal, and calling f.fly() runs whichever implementation the object's actual class provides. This lets code be written entirely against an interface type, accepting any implementing class without ever needing to know or check which specific one it actually received — the same polymorphic benefit inheritance offers, but without requiring the implementing classes to share any common superclass at all.

This is especially powerful for otherwise-unrelated classes. A Bird and an Airplane share no meaningful inheritance relationship, but both can implement Flyable and be treated identically by any code written against that interface type.

@HowItWorks
1

An interface-typed reference to an implementing object

Flyable f = new Bird(); is legal, since Bird implements Flyable.

2

Which implementation actually runs

Calling f.fly() runs whichever version the object's real, actual class provides.

3

Code written against the interface stays generic

It never needs to check which specific implementing class it actually received.

4

Works across unrelated classes

A Bird and an Airplane need no inheritance relationship at all to both be treated as Flyable.

5

Collections of an interface type

A list declared as List<Flyable> can hold objects of any class that implements Flyable, mixed freely.

@Example
interface Flyable {
void fly();
}
 
class Bird implements Flyable {
public void fly() {
System.out.println("Bird flapping wings");
}
}
 
class Airplane implements Flyable {
public void fly() {
System.out.println("Airplane using jet engines");
}
}
 
public class InterfacePolyDemo {
public static void main(String[] args) {
Flyable[] flyers = { new Bird(), new Airplane() };
for (Flyable f : flyers) {
f.fly();
}
}
}
Output
Bird flapping wings
Airplane using jet engines
1flyers is an array typed as Flyable, holding one Bird and one Airplane — two entirely unrelated classes otherwise.
2The for-each loop treats every element identically, as just "something Flyable," regardless of its real class.
3Each call to f.fly() runs whichever implementation actually belongs to that element's real class.
4Two lines print, each from a different class's own version of fly(), through the exact same loop code.
@UseCases
Treating otherwise-unrelated classes uniformly, as long as they share one implemented interface
Writing a method or loop that works with any Flyable, Comparable, or other interface type, generically
Storing a mix of different implementing classes together in one interface-typed collection
Designing code around behavior contracts instead of concrete class hierarchies

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.