1. Introduction
Both Interface and Abstract Class are used to achieve abstraction in Java, but they differ in:
- Design purpose
- Implementation
- Flexibility
Understanding this difference is very important for interviews and real-time design.
2. Abstract Class
Explanation
-
An abstract class can have:
- Abstract methods (without body)
- Concrete methods (with implementation)
Can have constructors, variables, and method implementations
Supports partial abstraction (0–100%)
Example
abstract class Vehicle {
abstract void start();
void fuel() {
System.out.println("Fuel type: Petrol/Diesel");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with key");
}
}
Explanation of Code
-
Vehicledefines common behavior -
Carprovides implementation for abstract method - Allows code reuse
3. Interface
Explanation
- An interface provides 100% abstraction (traditionally)
-
Contains:
- Abstract methods
- (Java 8+) default and static methods
Variables are public static final by default
Supports multiple inheritance
Example
interface Payment {
void pay();
}
class UpiPayment implements Payment {
public void pay() {
System.out.println("Paid using UPI");
}
}
Explanation of Code
-
Paymentdefines contract -
UpiPaymentimplements it - Focus is on what to do, not how
4. Key Differences Table
| Feature | Abstract Class | Interface |
|---|---|---|
| Abstraction | Partial (0–100%) | 100% (mostly) |
| Methods | Abstract + Concrete | Abstract (default/static allowed) |
| Variables | Instance variables allowed | Only public static final |
| Constructors | Yes | No |
| Inheritance | Single inheritance | Multiple inheritance |
| Keywords | extends |
implements |
| Performance | Slightly faster | Slight overhead |
| Use Case | Common base class | Contract definition |
5. Real-Time Use Case
Abstract Class
- When classes share common functionality
-
Example:
- Vehicle → Car, Bike
Interface
- When different classes share common behavior
-
Example:
- Payment → UPI, CreditCard, NetBanking
6. When to Use What?
-
Use Abstract Class when:
- You need code reuse
- You have a strong is-a relationship
-
Use Interface when:
- You want loose coupling
- You need multiple inheritance
- You define a contract
7. Summary
- Abstract Class → Partial abstraction + implementation
- Interface → Full abstraction + contract
- Abstract class = “IS-A” relationship
- Interface = “CAN-DO” capability
Java Full Stack Developer Roadmap
To master OOP concepts like interfaces and abstract classes:
👉 https://www.ashokit.in/java-full-stack-developer-roadmap
Promotional Content
Want to master OOP concepts like Interface vs Abstract Class?
Top comments (0)