ReviseAlgo Logo

Abstraction

Abstract Classes

Master abstract class specifications, partial state models, subclass instantiation rules, and parent constructor execution.

Interview: Frequently tested: abstract class constructor invocation mechanics, memory allocation during subclass construction, and final vs abstract constraints.

Last Updated: June 13, 2026 10 min read

An Abstract Class is a class declared with the abstract modifier. It serves as a partial template, allowing you to define shared instance fields (state) and concrete methods while leaving specific methods abstract for subclasses to implement.

Partial State Support

Unlike interfaces, abstract classes can hold instance fields (state), enabling them to manage subclass lifecycle and shared instance properties.

Constructors Exist

Abstract classes declare constructors to initialize parent fields during subclass construction, triggered via super() chaining.

No Direct Instance

Using new directly on an abstract class is prohibited by the compiler. Instantiation is only possible through concrete subclasses.

Why Do Abstract Classes Have Constructors?

Since abstract classes cannot be instantiated using the new keyword, developers often wonder why they contain constructors:

  • State Initialization: Abstract classes can hold instance variables (e.g., id, creationDate). The abstract class constructor is responsible for setting these values.
  • Constructor Chaining: When a concrete subclass is instantiated, its constructor runs. The very first statement invokes the parent class constructor (via super()) to guarantee that parent fields are fully allocated and initialized in memory first.

Memory Layout in Subclass Heap

When a subclass of an abstract class is created, the JVM allocates a single contiguous block of heap memory containing both the parent's instance fields and the subclass's fields. The abstract class variables occupy physical space inside the subclass object on the heap.

Common Pitfalls

  • Missing No-Arg Constructor in Base: If an abstract class defines only parameterized constructors, subclasses must explicitly invoke super(args). Forgetting this yields a compilation error.
  • Declaring Abstract Class as Final: Trying to combine abstract and final modifiers on a class. This is a compile error because final prevents subclassing, while abstract requires it.

Best Practices

  • Expose Common State in Abstract Class: Store common variables (e.g. database connections, configuration keys) in the abstract base class to avoid code duplication in subclasses.
  • Mark Fields as Private or Protected: Encapsulate base fields properly. Expose them to subclasses using protected getter/setter methods.

Interview-Relevant Information

Q1: Can we instantiate an abstract class? What happens under the hood when we declare an anonymous subclass?
Answer: No, we cannot instantiate an abstract class directly. When we write Base b = new Base() { ... };, we are not instantiating the abstract class. Instead, the compiler generates an anonymous concrete subclass that extends the abstract class, and instantiates that child subclass.

Q2: Can an abstract class have zero abstract methods?
Answer: Yes. An abstract class is not required to contain abstract methods. Marking it abstract simply prevents direct instantiation, which is useful when the class is conceptual and should only serve as a base for extension.

Quick Checklist

Can you explain constructor execution flow in abstract classes, verify subclass heap allocations, and explain why abstract and final cannot be combined? If yes, you understand abstract classes.

Use Cases

Defining base domain entities with common identifiers and lifecycle fields (e.g. BaseEntity with ID and created timestamp).

Creating reusable templates using the Template Method design pattern.

Common Mistakes

Attempting to declare an abstract class as final or private.

Forgetting to call the parameterized parent constructor explicitly when no default constructor exists in the base class.