Skip to main content

Day 5 : Abstraction

Published 3 min readMarkdown

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:

  1. Using Abstract Classes

  2. Using Interfaces

1️⃣ Using Abstract Classes

Steps to Achieve Abstraction

  1. Create an abstract class using the abstract keyword.

  2. Define abstract methods (methods without body) for behaviors you want to hide.

  3. Add concrete methods (optional) if you want some default implementation.

  4. Create a subclass that extends the abstract class.

  5. Override all abstract methods in the subclass.

  6. 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

  1. Create an interface using the interface keyword.

  2. Declare abstract methods (methods without body).

  3. Create classes that implement the interface.

  4. Override all abstract methods in implementing classes.

  5. 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

FeatureAbstract ClassInterface
MethodsAbstract + ConcreteAbstract (default/static allowed)
Multiple InheritanceNot allowedAllowed
ConstructorAllowedNot allowed
VariablesYesBy 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

No comments yet. Be the first to comment.

More from this blog