What is Abstraction in Java with Examples & Its Uses

Abstraction in Java means hiding the internal implementation details of a feature and showing only the necessary information to the user. It focuses on what an object does rather than how it does it.

Ways to Achieve Abstraction

1. Abstract Classes

abstract class Shape {
    abstract double area();

    void display() {
        System.out.println("This is a shape");
    }
}

class Circle extends Shape {
    double radius = 5;
    double area() {
        return Math.PI * radius * radius;
    }
}

2. Interfaces

interface Payment {
    void pay(double amount);
}

class CreditCardPayment implements Payment {
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using credit card");
    }
}

Abstract Class vs Interface

  • An abstract class can have both abstract and concrete methods; an interface (traditionally) only declares methods
  • A class can extend only one abstract class but can implement multiple interfaces
  • Abstract classes can have constructors and instance variables; interfaces cannot have instance state

Real-World Uses of Abstraction

  • Defining a common contract for different payment gateways, database drivers, or file readers
  • Building frameworks and APIs where implementation details can change without affecting the calling code
  • Simplifying complex systems by exposing only relevant operations to the user
Abstraction and encapsulation often go hand in hand — encapsulation hides data, while abstraction hides implementation complexity.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course