Interface In Java
An in Java is used to define a common set of methods that a class must implement. An interface helps in achieving abstraction and supports multiple inheritance. In this chapter, we will learn how to define an interface, how to achieve abstraction and multiple inheritance using interfaces.
An interface is a blueprint of a class that contains static constants and abstract methods. Interfaces are used to achieve abstraction. An interface contains only abstract methods (methods without a body) and variables. It cannot be instantiated, similar to an abstract class, and represents an IS-A relationship.
Since Java 8, interfaces can include default and static methods. Since Java 9, interfaces can also contain private methods.
Why Use an Interface in Java?
1. Achieve Abstraction
Interfaces help hide implementation details and expose only behavior.
Example
interface Payment {
void pay(double amount);
}
Different payment systems can implement this differently:
class CreditCardPayment implements Payment {
public void pay(double amount) {
System.out.println("Paid using credit card");
}
}
class UpiPayment implements Payment {
public void pay(double amount) {
System.out.println("Paid using UPI");
}
}
The user only knows:
Payment p = new UpiPayment();p.pay(500);
They don’t care how payment happens internally.
Benefit
- Cleaner code
- Reduced complexity
- Better security
2. Support Multiple Inheritance
Java does not allow multiple inheritance with classes because of ambiguity problems.
class A {}
class B {}
// class C extends A, B ❌ Not allowed
But a class can implement multiple interfaces:
interface Camera {
void click();
}
interface MusicPlayer {
void playMusic();
}
class Smartphone implements Camera, MusicPlayer {
public void click() {
System.out.println("Photo clicked");
}
public void playMusic() {
System.out.println("Playing music");
}
}
Benefit
A class can gain multiple capabilities safely.
3. Enable Loose Coupling
Loose coupling means classes depend on interfaces instead of concrete classes.
Tight Coupling ❌
class Engine {
void start() {}
}
class Car {
Engine engine = new Engine();
}
Car is directly dependent on Engine.
Loose Coupling ✅
interface Engine {
void start();
}
class PetrolEngine implements Engine {
public void start() {
System.out.println("Petrol engine started");
}
}
class ElectricEngine implements Engine {
public void start() {
System.out.println("Electric engine started");
}
}
class Car {
private Engine engine;
Car(Engine engine) {
this.engine = engine;
}
void drive() {
engine.start();
}
}
Now Car works with any engine implementation.
Benefit
- Easy to replace components
- Easier testing
- More flexible design
4. Improve Code Reusability
Interfaces allow writing generic code.
Example:
interface Shape {
double area();
}
Many shapes can implement it:
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle implements Shape {
double length, width;
Rectangle(double l, double w) {
length = l;
width = w;
}
public double area() {
return length * width;
}
}
Generic method:
void printArea(Shape s) {
System.out.println(s.area());
}
Works for all shapes.
5. Help in Polymorphism
Interfaces are heavily used for runtime polymorphism.
Animal a = new Dog();
a.sound();
Later:
a = new Cat();
a.sound();
Same interface reference, different behavior.
Benefit
Makes programs dynamic and extensible.
6. Standardize Behavior
Interfaces define a contract.
Example:
interface Printable {
void print();
}
Any class implementing Printable MUST provide print().
Benefit
Ensures consistency across different classes.
7. Essential for Frameworks and APIs
Many Java frameworks rely heavily on interfaces.
Examples from Java:
RunnableComparableListMapSet
Example:
List<String> list = new ArrayList<>();
Why use List instead of ArrayList?
Because later you can change implementation easily:
List<String> list = new LinkedList<>();
Without changing the rest of the code.
8. Useful for Dependency Injection
Frameworks like Spring Framework use interfaces extensively.
Example:
interface NotificationService {
void send(String msg);
}
Implementations:
class EmailService implements NotificationService {}
class SmsService implements NotificationService {}
Spring can inject any implementation dynamically.
9. Easier Unit Testing and Mocking
Interfaces make testing simpler.
Example:
interface Database {
void save();
}
Real database:
class MySQLDatabase implements Database {}
Test database:
class FakeDatabase implements Database {}