Abstraction
Dashboard
Topic 12/30
Home › Abstraction

🎨 Abstraction

Explore abstract classes and abstract methods in Java OOP.

What is Abstraction?

Abstraction is the process of hiding the implementation details and showing only the essential features or functionality to the user. In Java, abstraction is achieved using abstract classes (0 to 100% abstraction) and interfaces (100% abstraction).

Abstract Class Rules

  • A class declared with the abstract keyword is an abstract class.
  • It cannot be instantiated directly (cannot use new).
  • It can contain both abstract methods (without body) and concrete methods (with body).
  • If a class has at least one abstract method, the class itself must be declared abstract.
  • Subclasses must implement all abstract methods of the parent, or the subclass must also be declared abstract.

Abstract Class vs Interface

Abstract ClassInterface
Can have abstract and non-abstract methods.Only abstract methods (until Java 8 introduced default/static methods).
Can have instance variables (state).Only final, static variables (constants).
Supports single inheritance.Supports multiple inheritance.
Can have constructors.Cannot have constructors.

Basic Abstraction Example

VehicleDemo.javaJava
abstract class Vehicle {
    // Abstract method (no body)
    abstract void startEngine();

    // Concrete method
    void stopEngine() {
        System.out.println("Engine stopped.");
    }
}

class Car extends Vehicle {
    @Override
    void startEngine() {
        System.out.println("Car engine starts with a button.");
    }
}

class Bike extends Vehicle {
    @Override
    void startEngine() {
        System.out.println("Bike engine starts with a kick.");
    }
}

public class VehicleDemo {
    public static void main(String[] args) {
        Vehicle myCar = new Car();
        myCar.startEngine();
        myCar.stopEngine();

        Vehicle myBike = new Bike();
        myBike.startEngine();
    }
}

Abstract Class with Constructors

Abstract classes can have constructors. These are called when a concrete subclass is instantiated.

AbstractAnimal.javaJava
abstract class Animal {
    String name;
    
    Animal(String name) {
        this.name = name;
        System.out.println("Animal constructor called for " + name);
    }

    abstract void eat();
    abstract void sleep();
}

class Lion extends Animal {
    Lion(String name) {
        super(name);
    }

    @Override
    void eat() { System.out.println(name + " eats meat."); }

    @Override
    void sleep() { System.out.println(name + " sleeps in a den."); }
}

public class AbstractAnimal {
    public static void main(String[] args) {
        Lion leo = new Lion("Leo");
        leo.eat();
        leo.sleep();
    }
}

Template Method Pattern

Abstract classes are widely used in the Template Method Pattern, where the abstract base class dictates the overall algorithm flow, but delegates specific steps to subclasses.

DatabaseConnector.javaJava
abstract class DataProcessor {
    // Template method defining the skeleton
    public final void processData() {
        connect();
        extractData();
        disconnect();
    }

    abstract void connect();
    abstract void extractData();
    
    void disconnect() {
        System.out.println("Disconnecting from database...");
    }
}

class MySQLProcessor extends DataProcessor {
    @Override
    void connect() { System.out.println("Connecting to MySQL..."); }

    @Override
    void extractData() { System.out.println("Extracting rows from MySQL..."); }
}

public class DatabaseConnector {
    public static void main(String[] args) {
        DataProcessor processor = new MySQLProcessor();
        processor.processData(); // Executes the template flow
    }
}

Abstraction Use Case: Payment Systems

PaymentGateway.javaJava
abstract class Payment {
    double amount;
    
    Payment(double amount) {
        this.amount = amount;
    }

    abstract void processPayment();
}

class CreditCardPayment extends Payment {
    CreditCardPayment(double amount) { super(amount); }
    
    @Override
    void processPayment() {
        System.out.println("Processing Credit Card payment of $" + amount);
    }
}

class PayPalPayment extends Payment {
    PayPalPayment(double amount) { super(amount); }
    
    @Override
    void processPayment() {
        System.out.println("Processing PayPal payment of $" + amount);
    }
}

public class PaymentGateway {
    public static void main(String[] args) {
        Payment p1 = new CreditCardPayment(150.00);
        Payment p2 = new PayPalPayment(45.50);
        
        p1.processPayment();
        p2.processPayment();
    }
}