Day 5 : Abstraction
What is Abstraction?
Abstraction is the process of hiding implementation details and showing only the essential features of an object.
Focus: What an object does, not how it does it.
Helps in reducing complexity and increasing code maintainability.
Real-life analogy:
- Driving a car: You know how to use the steering wheel, accelerator, and brakes. You don’t need to know how the engine works internally.
How to Achieve Abstraction in Java
Abstraction can be achieved in two main ways:
Using Abstract Classes
Using Interfaces
1️⃣ Using Abstract Classes
Steps to Achieve Abstraction
Create an abstract class using the abstract keyword.
Define abstract methods (methods without body) for behaviors you want to hide.
Add concrete methods (optional) if you want some default implementation.
Create a subclass that extends the abstract class.
Override all abstract methods in the subclass.
Create an object of the subclass (not the abstract class) to access methods.
Real-Life Example: Vehicle
Abstract class: Vehicle
Abstract method: start() → all vehicles start differently
Concrete method: info() → displays general info about vehicles
// Abstract class
abstract class Vehicle {
// Abstract method (must be implemented by subclasses)
abstract void start();
// Concrete method (can be used as-is by subclasses)
void info() {
System.out.println("This is a vehicle");
}
}
// Subclass implementing abstract behavior
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with key or button");
}
}
// Main class to test abstraction
public class Main {
public static void main(String[] args) {
Vehicle v = new Car(); // Abstraction in action: Vehicle reference, Car object
v.start(); // Calls Car's implementation
v.info(); // Calls Vehicle's concrete method
}
}
✅ Explanation:
You don’t need to know how the Car starts internally—you just call start(). This is abstraction.
2️⃣ Using Interfaces
Steps to Achieve Abstraction
Create an interface using the interface keyword.
Declare abstract methods (methods without body).
Create classes that implement the interface.
Override all abstract methods in implementing classes.
Use interface reference to point to implementing objects.
Real-Life Example: Payment
Interface: Payment
Abstract method: pay(amount) → different payment methods implemented differently
// Interface defining a contract
interface Payment {
void pay(double amount); // abstract behavior
}
// Implementation of Payment using Credit Card
class CreditCard implements Payment {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Credit Card");
}
}
// Implementation of Payment using PayPal
class PayPal implements Payment {
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using PayPal");
}
}
// Main class to test interface implementation
public class Main {
public static void main(String[] args) {
Payment p1 = new CreditCard(); // Interface reference, CreditCard object
Payment p2 = new PayPal(); // Interface reference, PayPal object
p1.pay(1000); // Calls CreditCard's pay
p2.pay(500); // Calls PayPal's pay
}
}
✅ Explanation:
You don’t need to know how each payment is processed internally. You just call pay(). This is abstraction.
Difference Between Abstract Class and Interface
| Feature | Abstract Class | Interface |
| Methods | Abstract + Concrete | Abstract (default/static allowed) |
| Multiple Inheritance | Not allowed | Allowed |
| Constructor | Allowed | Not allowed |
| Variables | Yes | By default public static final |
Summary
Abstraction = Hiding implementation, showing functionality.
Use abstract classes for partial abstraction.
Use interfaces for full abstraction and multiple inheritance.
Makes code modular, reusable, and easier to maintain.
Summary Table: Steps with Real-Life Analogy
Method | Steps | Real-Life Analogy |
Abstract Class | 1. Create abstract class | Vehicle: start() is different for Car/Bike, info() is common |
Interface | 1. Create interface | Payment: CreditCard and PayPal implement pay() differently |
Comments
Join the discussionNo comments yet. Be the first to comment.