DEV Community

Cover image for Abstraction in Java with Real-Time Example
Sharath Kumar
Sharath Kumar

Posted on

Abstraction in Java with Real-Time Example

1. Introduction

Abstraction is one of the core concepts of Object-Oriented Programming (OOP).

πŸ‘‰ It means hiding internal implementation details and showing only the essential features to the user.

In simple terms:
β€œWhat to do” is exposed, but β€œhow it is done” is hidden.


2. Real-Time Analogy

Think of an ATM Machine:

  • You insert a card and withdraw money
  • You don’t know how:

    • Bank server validates PIN
    • Balance is checked
    • Cash is processed

πŸ‘‰ You only see operations, not internal logic β†’ This is abstraction


3. How Abstraction is Achieved in Java

Abstraction can be achieved using:

  1. Abstract Class (0–100% abstraction)
  2. Interface (100% abstraction)

4. Example Using Abstract Class

// Abstract class
abstract class Vehicle {
    abstract void start();  // abstract method

    void fuelType() {
        System.out.println("Petrol/Diesel");
    }
}

// Concrete class
class Car extends Vehicle {
    void start() {
        System.out.println("Car starts with key ignition");
    }
}

public class AbstractionExample {
    public static void main(String[] args) {
        Vehicle v = new Car();
        v.start();
        v.fuelType();
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Explanation of Code

  • Vehicle is an abstract class
  • start() is an abstract method β†’ no implementation
  • Car provides the implementation of start()
  • User interacts with Vehicle reference β†’ doesn't know internal logic

Output:

Car starts with key ignition
Petrol/Diesel
Enter fullscreen mode Exit fullscreen mode

6. Example Using Interface

interface Payment {
    void pay();
}

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

class UpiPayment implements Payment {
    public void pay() {
        System.out.println("Paid using UPI");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Payment p = new UpiPayment();
        p.pay();
    }
}
Enter fullscreen mode Exit fullscreen mode

7. Explanation of Code

  • Payment interface defines method pay()
  • Different classes implement it differently
  • User only calls pay() β†’ doesn't care about internal implementation

8. Key Points

  • Hides implementation details
  • Shows only functionality
  • Improves security and flexibility
  • Helps in loose coupling

9. Advantages of Abstraction

  • Reduces complexity
  • Improves maintainability
  • Enhances reusability
  • Allows changing implementation without affecting users

10. Summary

  • Abstraction = hiding internal details
  • Achieved using abstract classes and interfaces
  • Focuses on what to do, not how to do
  • Widely used in real-time applications

Java Full Stack Developer Roadmap

To master OOP concepts like abstraction, inheritance, and polymorphism:

πŸ‘‰ https://www.ashokit.in/java-full-stack-developer-roadmap


Promotional Content

Want to master OOP concepts like Abstraction with real-time examples?

Best Core JAVA Online Training in ameerpet

Top comments (0)