← Back to Home

Abstract Classes

An abstract class in Java is a partially implemented class that provides abstraction by defining what a class should do, while allowing subclasses to decide how to do it. It is used when classes share common behavior but must implement specific details differently. This is a high-frequency interview topic, often compared with interfaces.

What Is an Abstract Class?

  • Declared using the abstract keyword
  • Can contain abstract methods (no body)
  • Can contain concrete methods (with body)
  • Cannot be instantiated
  • Supports inheritance-based abstraction
abstract class Vehicle {
    abstract void start();
}
          

Why Abstract Classes Are Needed

  • Avoid code duplication
  • Share common functionality
  • Enforce method implementation in subclasses
  • Provide partial abstraction
  • Improve design consistency

Abstract Method

An abstract method has no implementation and must be implemented by subclasses.

abstract void start();
          
  • ✔ Ends with semicolon
  • ✔ No method body

Abstract Class Example (Basic)

abstract class Vehicle {

    abstract void start();   // abstract method

    void fuel() {            // concrete method
        System.out.println("Fueling vehicle");
    }
}

class Car extends Vehicle {

    @Override
    void start() {
        System.out.println("Car starts with key");
    }
}

Vehicle v = new Car();
v.start();
v.fuel();
          

✔ Abstraction + polymorphism

Key Rules of Abstract Classes (Very Important)

  1. Cannot create an object of an abstract class
// Vehicle v = new Vehicle(); // ❌
          
  1. Can have abstract and non-abstract methods
  2. Can have constructors
abstract class A {
    A() {
        System.out.println("Constructor");
    }
}
          
  1. Can have instance variables
  2. Can extend only one class
  3. A subclass must implement all abstract methods (unless the subclass is also abstract)

Abstract Class with Constructor

abstract class Shape {
    Shape() {
        System.out.println("Shape created");
    }
    abstract double area();
}

class Circle extends Shape {
    double radius;

    Circle(double r) {
        radius = r;
    }

    double area() {
        return 3.14 * radius * radius;
    }
}
          

✔ Constructors run from parent to child

Abstract Class with Partial Implementation

abstract class Bank {

    abstract double getRateOfInterest();

    double calculateInterest(double amount) {
        return amount * getRateOfInterest() / 100;
    }
}
          

✔ Common logic reused
✔ Rate differs per bank

Abstract Class vs Concrete Class

Feature Abstract Class Concrete Class
Instantiation ❌ No ✅ Yes
Abstract methods Allowed ❌ Not allowed
Implementation Partial Full
Use case Base design Actual object

Abstract Class vs Interface (Quick Preview)

Feature Abstract Class Interface
Methods Abstract + concrete Abstract (default allowed)
Variables Instance variables Constants only
Constructors Yes No
Multiple inheritance ❌ No ✅ Yes
Use case IS-A relationship Capability/contract

When to Use Abstract Classes

  • When classes share common state and behavior
  • When partial implementation is needed
  • When you want to provide base functionality
  • When multiple related classes exist

When NOT to Use Abstract Classes

  • When multiple inheritance is required
  • When only method contracts are needed
  • When no common code exists

Common Beginner Mistakes

  • Trying to create object of abstract class
  • Forgetting to implement abstract methods
  • Confusing abstract class with interface
  • Using abstract class where interface is better
  • Overusing abstraction

Interview-Ready Answers

Short Answer

An abstract class is a class that cannot be instantiated and may contain abstract and concrete methods.

Detailed Answer

In Java, an abstract class is used to achieve abstraction by providing partial implementation. It can contain abstract methods that must be implemented by subclasses and concrete methods with shared logic. Abstract classes support inheritance and polymorphism.

Key Takeaway

Abstract classes define a base blueprint with shared behavior and mandatory methods. They are ideal when you need partial abstraction with code reuse.