Java Abstraction
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Abstraction

Harine

 Java Abstraction

Abstraction is a core principle of object-oriented programming (OOP) that focuses on hiding implementation details and exposing only the essential features of an object. It helps developers design clean, secure, and maintainable systems by separating what an object does from how it does it.

In Java, abstraction is achieved using:

  • Abstract classes
  • Interfaces (covered in the next chapter)

What Is Abstraction in Java?

Abstraction means showing only relevant behavior to users while hiding internal complexity.

Real-world analogy:

You can drive a car using the steering wheel and pedals without knowing how the engine works internally.

Abstract Class in Java

An abstract class is a class declared with the abstract keyword. It cannot be instantiated directly and is meant to be extended by subclasses.

Key characteristics

  • Cannot create objects
  • Can contain abstract and concrete methods
  • Can have fields and constructors
  • Must be inherited to be used

Syntax

abstract class ClassName {

  abstract void abstractMethod(); // no body

  void concreteMethod() {

     // implementation

  }

}

Abstract Method in Java

An abstract method is declared without a body and must be implemented by subclasses.

abstract void methodName();

If a class contains at least one abstract method, the class itself must be declared abstract.

Example: Abstract Class with Subclass Implementation

abstract class Payment {

  protected double amount;

  public Payment(double amount) {

    this.amount = amount;

  }

  public abstract void processPayment(); // abstract method

  public void printReceipt() {

    System.out.println("Payment of ₹" + amount + " processed.");

  }

}

class CreditCardPayment extends Payment {

  private String cardNumber;

  public CreditCardPayment(double amount, String cardNumber) {

    super(amount);

    this.cardNumber = cardNumber;

  }

  @Override

  public void processPayment() {

    System.out.println("Processing credit card payment using card " + cardNumber);

  }

}

public class AbstractionDemo {

  public static void main(String[] args) {

    CreditCardPayment payment = new CreditCardPayment(2500, "****-****-1234");

    payment.processPayment();

    payment.printReceipt();

   }

}

Output

Processing credit card payment using card ****-****-1234

Payment of ₹2500.0 processed.

Why Abstract Classes Cannot Be Instantiated

abstract class Vehicle {

  abstract void start();

}

// Not allowed

Vehicle v = new Vehicle();

Reason:

Abstract classes may contain incomplete behavior (abstract methods), so Java prevents direct object creation.

When to Use Abstract Classes

Use abstract classes when:

  • Classes share common structure and behavior
  • Some behavior must be enforced in subclasses
  • You want partial implementation reuse
  • You want a base template for related objects

Examples:

  • Payment systems
  • Vehicles
  • UI components
  • Employees
  • Devices

Benefits of Abstraction

  • Hides complexity
  • Improves security
  • Enforces design consistency
  • Enables polymorphism
  • Supports scalable architecture

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !