Abstraction
Abstraction is an object-oriented programming (OOP) principle that focuses on exposing only essential features of an object while hiding the internal implementation details. It helps in building clean, flexible, and maintainable systems.
This is a high-frequency interview topic, especially when comparing abstract classes vs interfaces.
What Is Abstraction?
- Showing what an object does
- Hiding how it does it
- Focuses on behavior, not implementation
- Achieved using abstract classes and interfaces
Simple definition: Abstraction hides complexity and exposes functionality.
Why Abstraction Is Important
- Reduces complexity
- Improves maintainability
- Enhances flexibility
- Supports loose coupling
- Enables polymorphism
Real-World Analogy
Car
- You use steering, accelerator, brakes
- You don’t care how the engine works internally
This is abstraction.
How Abstraction Is Achieved in Java
Java provides two mechanisms:
- Abstract Classes
- Interfaces
1️⃣ Abstract Class
An abstract class is a class declared using the abstract keyword. It can contain:
- Abstract methods (no body)
- Concrete methods (with body)
- Instance variables
- Constructors
Abstract Class Example
abstract class Vehicle {
abstract void start(); // abstract method
void fuel() { // concrete method
System.out.println("Fueling vehicle");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with key");
}
}
Key Rules of Abstract Classes
- Cannot be instantiated
- May contain abstract and non-abstract methods
- Can have constructors
- Can have instance variables
- Can extend one class only
2️⃣ Interface
An interface defines a contract that classes must implement.
interface Payment {
void pay();
}
class CreditCardPayment implements Payment {
@Override
public void pay() {
System.out.println("Credit card payment");
}
}
Key Rules of Interfaces
- All methods are public and abstract by default
- Supports multiple inheritance
- No constructors
- Variables are public static final
Abstract Class vs Interface (Interview Favorite)
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Abstract + concrete | Abstract (default allowed Java 8+) |
| Variables | Instance variables | Constants only |
| Constructors | Yes | No |
| Multiple inheritance | ❌ No | ✅ Yes |
| Access modifiers | Any | Public only |
| Use case | IS-A relationship | CAN-DO capability |
Abstraction vs Encapsulation (Common Confusion)
| Aspect | Abstraction | Encapsulation |
|---|---|---|
| Focus | What | How |
| Purpose | Hide complexity | Protect data |
| Achieved by | Abstract class / interface | Access modifiers |
| Level | Design | Implementation |
Abstraction with Polymorphism
Payment p = new CreditCardPayment();
p.pay();
- Interface reference
- Implementation decided at runtime
When to Use Abstraction
- Framework design
- APIs and libraries
- Large systems
- When behavior varies but contract stays same
Common Beginner Mistakes
- Confusing abstraction with encapsulation
- Overusing abstract classes
- Forgetting to implement abstract methods
- Trying to instantiate abstract classes
- Ignoring interfaces for multiple inheritance
Interview-Ready Answers
Short Answer
Abstraction is the concept of hiding implementation details and showing only essential features.
Detailed Answer
In Java, abstraction is achieved using abstract classes and interfaces. It focuses on defining a contract and hiding internal implementation, enabling loose coupling, flexibility, and easier maintenance.
Key Takeaway
Abstraction defines the contract. Implementation provides the details. Abstraction is essential for scalable, extensible, and maintainable Java applications.