Skip to the content.

Fullstack > Java > 🎨 Abstraction

Understanding Abstraction in Java

Abstraction is one of the four fundamental Object-Oriented Programming (OOP) principles in Java, alongside Encapsulation, Inheritance, and Polymorphism. It is the process of hiding implementation details from the user and exposing only the essential features.

In Java, abstraction is achieved through abstract classes and interfaces. It helps in reducing complexity and increasing the reusability of code.

Advantages of Abstraction


1. Abstraction using Abstract Classes

An abstract class in Java is a class that cannot be instantiated. It may contain abstract methods (methods without a body) and concrete methods (fully defined methods).

Example of Abstract Class:

abstract class Vehicle {
    abstract void start(); // Abstract method (no implementation)
    
    void stop() {
        System.out.println("Vehicle is stopping"); // Concrete method
    }
}

class Car extends Vehicle {
    @Override
    void start() {
        System.out.println("Car is starting with a key");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle myCar = new Car(); // Upcasting
        myCar.start(); // Calls Car's implementation
        myCar.stop();  // Calls inherited method
    }
}

Key Points:


2. Abstraction using Interfaces

An interface in Java is a blueprint that contains only abstract methods (before Java 8) and can also include default or static methods (from Java 8 onward). Classes that implement an interface must define all of its methods.

Example of Interface:

interface Payment {
    void pay(); // Abstract method
}

class CreditCardPayment implements Payment {
    @Override
    public void pay() {
        System.out.println("Payment made using Credit Card");
    }
}

class PayPalPayment implements Payment {
    @Override
    public void pay() {
        System.out.println("Payment made using PayPal");
    }
}

public class Main {
    public static void main(String[] args) {
        Payment payment1 = new CreditCardPayment();
        Payment payment2 = new PayPalPayment();
        
        payment1.pay(); // Calls CreditCardPayment's pay()
        payment2.pay(); // Calls PayPalPayment's pay()
    }
}

Key Points:


Abstract Class vs Interface

| Feature | Abstract Class | Interface | |———|—————|———–| | Methods | Can have both abstract and concrete methods | Only abstract methods (Java 7 and earlier); can have default/static methods (Java 8+) | | Inheritance | Single inheritance | Multiple inheritance | | Implementation | Subclasses extend the abstract class | Classes implement the interface | | Access Modifiers | Can have all types of access modifiers | Methods are public by default |


Conclusion


🔗 Related Topics: