[!NOTE] We learned earlier that Java enforces strict Single Inheritance. A child class can only
extendexactly ONE parent class. But what if yourBirdclass needs to inherit fromAnimal, but also needs to inherit a bunch of flying logic from aFlyableclass?
We solve this problem using Interfaces.
What is an Interface?
An Interface is an extreme version of an Abstract Class. Historically (prior to Java 8), an interface could only contain pure abstract methods and constant variables. It represents a strict "Contract of Behavior".
Instead of a class extending an interface, a class implements an interface. And unlike strict single inheritance, a class can implement an infinite number of interfaces simultaneously!
// 1. Defining Interfaces (Contracts)
interface Flyable {
void fly(); // Naturally abstract and public, even if we don't type it!
}
interface Swimable {
void swim();
}
// 2. A normal Parent Class
class Animal {
void sleep() { System.out.println("Zz"); }
}
// 3. The Ultimate Child Class
// It extends ONE parent, but implements TWO interfaces!
class Duck extends Animal implements Flyable, Swimable {
// Contract fulfillment: Must provide flight code!
public void fly() {
System.out.println("The duck flies awkwardly over the lake.");
}
// Contract fulfillment: Must provide swim code!
public void swim() {
System.out.println("The duck paddles in the water.");
}
}
Why Use Interfaces?
Interfaces decouple "what an object is" from "what an object can do".
A Helicopter is absolutely NOT an Animal. It makes zero sense for a Helicopter class to inherit from Animal. However, both a Helicopter and a Duck can fly. They share a behavioral capability.
If you have a video game function that accepts anything that can fly, you can use the Interface as the expected data type!
public class GameEngine {
// This method accepts ANYTHING that implements the Flyable interface!
// It doesn't care if it's a Duck, a Helicopter, or a MagicCarpet!
public void triggerTakeoff(Flyable target) {
target.fly();
}
}
[!TIP] Modern Java Updates: Since Java 8, interfaces are actually allowed to have
defaultmethods with real body implementations. This was added to allow architects to upgrade massive enterprise interfaces without breaking millions of legacy applications that hadn't written overrides for the new methods.
Interfaces Describe Capabilities
An interface is ideal when unrelated classes share a capability. A Bird, Drone, and Plane are not the same kind of object, but all can be Flyable. That makes interfaces a powerful way to design flexible code.
Programming to an Interface
interface PaymentMethod {
boolean pay(double amount);
}
class CardPayment implements PaymentMethod {
public boolean pay(double amount) {
System.out.println("Paid by card: " + amount);
return true;
}
}
class CheckoutService {
void checkout(PaymentMethod paymentMethod, double amount) {
paymentMethod.pay(amount);
}
}
CheckoutService does not care whether payment uses card, UPI, wallet, or net banking. It only cares that the object can pay.
Default Methods
interface Trackable {
void track();
default void printTrackingStarted() {
System.out.println("Tracking started");
}
}
Default methods are useful, but keep them small. Interfaces should mainly describe behavior, not become large utility classes.
Common Mistakes
- Naming interfaces too vaguely, such as
ManagerorHandler, without clear behavior. - Adding too many unrelated methods to one interface.
- Using an abstract class when unrelated classes only need to share a capability.
- Forgetting that interface methods are public contracts for implementers.
Mini Practice
Create an interface Downloadable with a download() method. Implement it in PdfFile and VideoFile. Write one method that accepts any Downloadable.
Practice Lab: Downloadable Files
Use an interface to model a shared capability across unrelated classes.
- Create interface
Downloadablewith methoddownload(). - Create
PdfFile,VideoFile, andAudioFile. - Make each class implement
Downloadable. - Write one method that accepts
Downloadableand callsdownload(). - Pass all three file types to that method.
Goal: Depend on capability, not concrete class names.
Revision Checkpoint
- Interface: Contract describing capability.
implements: Connects a class to an interface.- Multiple interfaces: One class can implement many interfaces.
- Capability design: Interfaces describe what an object can do.
- Default methods: Interfaces can provide small shared implementations.
Before the quiz: Name three unrelated classes that could implement the same interface.