Abstract Classes: Shared Structure with Required Pieces

Introduction

An abstract class cannot be instantiated directly. It defines shared code and abstract methods that subclasses must implement. Use abstract classes when types share implementation but still need customization—bridge to Interfaces.

Prerequisites

What Is an Abstract Class

java
public abstract class Animal {
    private final String name;
 
    protected Animal(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
 
    public abstract void makeSound();
 
    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}
java
// Animal a = new Animal("X");  // compile error — abstract
 
public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
 
    @Override
    public void makeSound() {
        System.out.println("Woof");
    }
}

1) Abstract Methods

No body in abstract class; child must implement (unless child is also abstract).

java
public abstract void makeSound();

Concrete class with unimplemented abstract methods → compile error.

2) Mix Concrete and Abstract

Abstract classes can include:

  • fields and constructors
  • concrete methods with implementation
  • abstract methods without implementation

This is the main difference from many interfaces (before default methods): abstract classes often carry shared state and code.

3) Template Method Pattern (Light)

Parent defines algorithm skeleton; children fill steps:

java
public abstract class DataExporter {
    public final void export() {
        open();
        writeData();
        close();
    }
 
    protected abstract void writeData();
 
    protected void open() {
        System.out.println("Opening connection...");
    }
 
    protected void close() {
        System.out.println("Closing connection...");
    }
}
 
public class CsvExporter extends DataExporter {
    @Override
    protected void writeData() {
        System.out.println("Writing CSV rows...");
    }
}

final export() prevents subclasses from breaking the workflow.

4) Abstract Class vs Interface (Preview)

Abstract classInterface
Instantiatenono
Multiple inheritanceone classmany interfaces
Fieldsyes (instance)constants + limited
Constructoryesno
Default method bodiesyes (concrete methods)yes (default since Java 8)

Full comparison: Interfaces.

Common Beginner Mistakes

Instantiating Abstract Class

Only concrete subclasses can be constructed with new.

Missing @Override on Implementations

Compiler still checks, but @Override catches signature typos early.

Abstract Class for Everything

If there is no shared implementation, an interface may be simpler.

Mini Practice

Abstract Payment with abstract boolean process(); subclasses CardPayment and CashPayment.

What’s Next

Root of all types: Object Class. Organize code: Packages.

FAQ

Can abstract methods be private?

No. Subclasses must see them to override—use protected or public.

Can an abstract class have a constructor?

Yes. Subclasses call it via super(...).

Abstract class vs interface — which first?

Prefer interface for pure contracts; abstract class when sharing fields and partial implementation among close relatives.

Can static methods be abstract?

No. Abstract applies to instance methods subclasses implement.

How does this relate to ranking record?

record is for data carriers; abstract classes model behavioral families.