Java Abstraction: Abstract Classes, Abstract Methods and Contracts

Abstraction is the fundamental OOP principle that hides complex internal implementation details and exposes only the essential operational features to the outside world. In Java, abstraction is achieved via Abstract Classes (0% to 100% partial abstraction) and Interfaces (100% pure abstraction).

What is an Abstract Class?

An abstract class is a class declared with the abstract keyword. It represents a conceptual base entity that cannot be instantiated directly. An abstract class can contain:

  • Abstract Methods: Methods with no body (no implementation) that must be implemented by concrete subclasses.
  • Concrete Methods: Fully implemented methods that provide shared default logic for all subclasses.
  • Instance & Static Variables: State fields that subclasses inherit.
  • Constructors: Initializers called when subclasses execute super().

Practical Example: Payment Processing Architecture

// Abstract Superclass
abstract class PaymentGateway {
    protected double amount;

    // Constructor in abstract class
    public PaymentGateway(double amount) {
        this.amount = amount;
    }

    // Concrete Method: Shared auditing logic
    public void logTransaction(String status) {
        System.out.println("[AUDIT LOG] Transaction for $" + amount + " Status: " + status);
    }

    // Abstract Method: Contract that subclasses MUST implement
    public abstract boolean processPayment();
}

// Concrete Subclass 1: Credit Card
class CreditCardPayment extends PaymentGateway {
    private String cardNumber;

    public CreditCardPayment(double amount, String cardNum) {
        super(amount);
        this.cardNumber = cardNum;
    }

    @Override
    public boolean processPayment() {
        System.out.println("Contacting Visa/Mastercard network for Card: " + cardNumber);
        logTransaction("SUCCESS_CREDIT_CARD");
        return true;
    }
}

// Concrete Subclass 2: UPI
class UpiPayment extends PaymentGateway {
    private String upiId;

    public UpiPayment(double amount, String upiId) {
        super(amount);
        this.upiId = upiId;
    }

    @Override
    public boolean processPayment() {
        System.out.println("Sending payment request to UPI VPA: " + upiId);
        logTransaction("SUCCESS_UPI");
        return true;
    }
}

University Exam Corner (BCA / B.Sc CS)

Exam Question: "If a class contains even one abstract method, must the entire class be declared abstract?"

Answer: Yes! If a class declares or inherits any abstract method without providing an implementation body, the class must explicitly be marked with the abstract keyword. Otherwise, the compiler throws an error: class must be declared abstract or implement abstract method.

Next Step

Now let us explore Java Interfaces, multiple inheritance, and modern default/static methods. Proceed to: Java Interfaces & Default Methods →

Frequently Asked Questions (FAQ)

Q1. Can an abstract class be instantiated directly using 'new'?
No! An abstract class is an incomplete blueprint and cannot be instantiated directly with 'new'. It can only be instantiated through its concrete subclasses that implement all abstract methods.
Q2. Can an abstract class have constructors in Java?
Yes! Abstract classes can have constructors. Although you cannot instantiate the abstract class directly, its constructor is automatically invoked via super() when a concrete subclass is instantiated.
Q3. Can an abstract method be declared static or final in Java?
No! Abstract methods are designed specifically to be overridden by subclasses. Declaring an abstract method as 'final' prevents overriding, and 'static' attaches it to the class, causing compilation errors.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Interfaces →

100% full abstraction contracts.

Polymorphism & Overriding →

Implementing abstract methods.

Java Inheritance →

Subclassing abstract superclasses.

OOP 4 Pillars →

Abstraction concept summary.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!