Java Interfaces
An interface in Java is a reference type used to define a contract of behavior that classes must implement. Interfaces are a key mechanism for achieving abstraction, loose coupling, and multiple inheritance of type in object-oriented design.
Unlike classes, interfaces specify what a class should do, not how it does it.
What Is an Interface in Java?
An interface is a collection of abstract methods (and constants) that implementing classes must provide concrete implementations for.
Basic syntax
interface InterfaceName {
void method1();
void method2();
}
A class uses the implements keyword to adopt an interface.
Implementing an Interface
When a class implements an interface, it must override all interface methods.
Example: Device interface implementation
interface SmartDevice {
void powerOn();
void powerOff();
}
class SmartLight implements SmartDevice {
@Override
public void powerOn() {
System.out.println("Smart light turned ON");
}
@Override
public void powerOff() {
System.out.println("Smart light turned OFF");
}
}
public class InterfaceDemo {
public static void main(String[] args) {
SmartDevice device = new SmartLight();
device.powerOn();
device.powerOff();
}
}
Output
Smart light turned ON
Smart light turned OFF
Key Properties of Java Interfaces
Interfaces have special default behaviors in Java:
- Methods are implicitly public and abstract
- Fields are implicitly public, static, and final
- Cannot be instantiated
- No constructors allowed
- Classes must implement all methods (unless abstract)
Why Use Interfaces in Java?
Interfaces provide several architectural advantages:
Benefits
- Achieve abstraction
- Enable multiple inheritance
- Promote loose coupling
- Support polymorphism
- Improve testability and scalability
- Define standard APIs
Multiple Interfaces in Java
Java does not support multiple class inheritance, but a class can implement multiple interfaces.
Example: Multiple interface implementation
interface Camera {
void takePhoto();
}
interface MusicPlayer {
void playMusic();
}
class Smartphone implements Camera, MusicPlayer {
@Override
public void takePhoto() {
System.out.println("Photo captured");
}
@Override
public void playMusic() {
System.out.println("Playing music");
}
}
public class MultiInterfaceDemo {
public static void main(String[] args) {
Smartphone phone = new Smartphone();
phone.takePhoto();
phone.playMusic();
}
}
Output
Photo captured
Playing music
Why Interfaces Enable Multiple Inheritance
A class can extend only one superclass:
class A {}
class B {}
// Not allowed
class C extends A, B {}
But it can implement multiple interfaces:
class C implements X, Y {}
This allows Java to support multiple behavior inheritance without ambiguity.
When to Use Interfaces
Use interfaces when:
- Multiple classes share common behavior
- You need multiple inheritance
- You define a capability or role
- You design APIs or frameworks
- You want loose coupling between components
Examples:
- Payment methods
- Logging systems
- Device drivers
- UI event listeners
- Service layers
Interface and Polymorphism
Interfaces enable polymorphic behavior:
SmartDevice d1 = new SmartLight();
SmartDevice d2 = new SmartLight();
d1.powerOn();
d2.powerOn();
The same interface reference can represent different implementations.