π° Introduction
In Java, an interface is a powerful feature used to define a contract that classes must follow. It plays a crucial role in:
- β Abstraction
- β Multiple inheritance
- β Loose coupling
- β Scalable system design
π Think of an interface as a rulebook β any class that agrees to it must implement all its methods.
π§© What is an Interface?
An interface contains:
- Abstract methods (by default)
- Constants (public static final)
interface Animal {
void makeSound(); // abstract method
}
π Behind the scenes:
public abstract void makeSound();
βοΈ How Interface Works
A class uses implements keyword to adopt an interface:
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
}
π‘ Explanation:
Dogagrees to followAnimal- Must implement
makeSound() - Method must be
public
π Full Working Example
interface Vehicle {
void start();
void stop();
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike starts");
}
public void stop() {
System.out.println("Bike stops");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Bike();
v.start();
v.stop();
}
}
π Output:
Bike starts
Bike stops
π This demonstrates:
- Runtime polymorphism
- Interface-based design
β‘ Types of Methods in Interface
1οΈβ£ Abstract Method (Default Behavior)
interface Test {
void show();
}
π Must be implemented in class.
2οΈβ£ Default Method (Java 8+)
interface Test {
default void display() {
System.out.println("Default method");
}
}
π Provides implementation inside interface.
3οΈβ£ Static Method
interface Test {
static void print() {
System.out.println("Static method");
}
}
π Called using:
Test.print();
4οΈβ£ Private Method (Java 9+)
interface Test {
private void helper() {
System.out.println("Helper method");
}
}
π Used internally within interface.
π Multiple Inheritance Using Interface
Java doesnβt allow multiple inheritance with classes, but interfaces make it possible.
π§© Example: Multiple Inheritance
interface A {
void show();
}
interface B {
void display();
}
class Demo implements A, B {
public void show() {
System.out.println("Show from A");
}
public void display() {
System.out.println("Display from B");
}
}
π‘ Explanation:
Demoimplements bothAandB- Must override both methods
- Achieves multiple inheritance safely
π Hybrid Inheritance Using Interfaces
π Hybrid inheritance = combination of multiple + hierarchical inheritance
π§© Example:
interface A {
void methodA();
}
interface B extends A {
void methodB();
}
interface C extends A {
void methodC();
}
class D implements B, C {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
public void methodC() {
System.out.println("Method C");
}
}
π‘ Explanation:
BandCextendAβ hierarchicalDimplements both β multiple- Combined = Hybrid inheritance
π§ Working of Abstract Method in Interface
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
π Flow:
- Interface defines method
- Class implements it
- Object calls method β runtime binding
π§ Working of Abstract Method in Abstract Class
abstract class Shape {
abstract void draw();
void info() {
System.out.println("Shape class");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
π Key Working:
- Abstract method must be implemented
- Concrete methods can be reused
βοΈ Interface vs Abstract Class (Deep Comparison)
π Detailed Table
| Feature | Interface | Abstract Class |
|---|---|---|
| πΉ Purpose | Contract | Base class |
| πΉ Methods | Abstract + Default + Static | Abstract + Concrete |
| πΉ Variables | Constants only | Instance variables allowed |
| πΉ Constructor | β No | β Yes |
| πΉ Inheritance | Multiple | Single |
| πΉ Access Modifiers | Public only (default) | Any |
| πΉ Speed | Slightly faster | Slightly slower |
| πΉ Use Case | Loose coupling | Code reuse |
π₯ Code Comparison
Interface
interface Animal {
void sound();
}
Abstract Class
abstract class Animal {
abstract void sound(); void eat() {
System.out.println("Eating...");
}
}
β οΈ Common Errors & Confusions
β 1. Missing public keyword
class Dog implements Animal {
void sound() {} // ERROR
}
βοΈ Fix:
public void sound() {}
β 2. Object creation of interface
Animal a = new Animal(); // β
βοΈ Use:
Animal a = new Dog();
β 3. Not implementing all methods
π Leads to compilation error
β 4. Confusion about variables
interface Test {
int x = 10;
}
π Actually:
public static final int x = 10;
π― When to Use Interface?
- πΉ When you need multiple inheritance
- πΉ When designing APIs
- πΉ When flexibility is required
- πΉ When behavior matters more than implementation
π― When to Use Abstract Class?
- πΉ When classes are closely related
- πΉ When you want shared code
- πΉ When you need constructors
π Conclusion
Interfaces are a cornerstone of modern Java development. They help in:
- β¨ Building flexible systems
- π Achieving polymorphism
- π§© Supporting multiple & hybrid inheritance
With modern enhancements (Java 8+), interfaces are no longer just abstract β they are powerful design tools.
π Key takeaway:
- Interface = What to do
- Abstract class = What + How