Interfaces
An interface in Java defines a contract that a class must follow. It specifies what a class should do without describing how it should do it.
Interfaces are a key building block of large-scale Java applications. They help teams write flexible, loosely coupled, and easily maintainable code.
Real-World Understanding of Interfaces
Think of a charging port. Different devices can use the same charging interface, but each device handles charging internally in its own way.
The interface guarantees compatibility, while the implementation remains flexible. This is exactly how Java interfaces work.
Why Java Uses Interfaces
Interfaces help Java developers:
- Achieve full abstraction
- Support multiple inheritance (which classes cannot)
- Reduce dependency between components
- Design scalable enterprise systems
Most Java frameworks are designed around interfaces rather than concrete classes.
Defining an Interface
An interface contains method declarations without implementation. Classes that implement the interface must provide implementations.
interface Payment {
void makePayment(double amount);
}
This interface defines a common rule for all payment types.
Implementing an Interface
Now let’s create classes that implement this interface.
class CreditCardPayment implements Payment {
public void makePayment(double amount) {
System.out.println("Paid " + amount + " using Credit Card");
}
}
class UpiPayment implements Payment {
public void makePayment(double amount) {
System.out.println("Paid " + amount + " using UPI");
}
}
Each class follows the same interface, but the payment process differs internally.
Using Interface Reference
Interfaces are most powerful when used as reference types.
public class Main {
public static void main(String[] args) {
Payment p1 = new CreditCardPayment();
Payment p2 = new UpiPayment();
p1.makePayment(1500);
p2.makePayment(800);
}
}
Java decides which implementation to execute at runtime. This supports polymorphism and clean design.
Multiple Inheritance Using Interfaces
Java does not support multiple inheritance with classes. However, a class can implement multiple interfaces.
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class MultiFunctionPrinter implements Printable, Scannable {
public void print() {
System.out.println("Printing document");
}
public void scan() {
System.out.println("Scanning document");
}
}
This allows Java to achieve multiple inheritance safely.
Interfaces in Real Applications
Interfaces are widely used in:
- Spring and Spring Boot
- Payment gateways
- Logging frameworks
- Database access layers
- Service-based architectures
They allow teams to swap implementations without breaking the system.
Key Differences: Abstract Class vs Interface
- Interfaces provide full abstraction
- A class can implement multiple interfaces
- Interfaces define behavior contracts
- Abstract classes can have state, interfaces cannot
Key Takeaways
- Interfaces define what a class must do
- They support polymorphism and loose coupling
- They enable multiple inheritance
- They are essential for modern Java design
In the next lesson, we will explore encapsulation, which focuses on protecting data and controlling access.