Course contentsShow
← Java
Lesson 478 of 2,87012. Abstract Classes and InterfacesFree lesson

The abstract Keyword for Classes

Declaring a class as abstract to prevent direct instantiation and signal that subclassing is required.

The abstract Keyword for Classes

You'll learn: How to use abstract to declare classes that cannot be instantiated directly and must be extended by subclasses.

What Is an Abstract Class?

An abstract class is a class marked with the abstract keyword that cannot be instantiated on its own. You cannot use new to create an object directly from an abstract class. Instead, abstract classes exist to be extended (subclassed), serving as templates or blueprints for more specific implementations.

Think of it like architectural plans: you can't live in a blueprint, but builders use it to construct actual houses. Similarly, you can't create instances of an abstract class directly, but you can create concrete subclasses from it.

Why Use abstract?

When you declare a class abstract, you're signaling two things:

  1. This class is incomplete – it represents a general concept that shouldn't exist on its own
  2. Subclassing is required – other classes must extend it to provide specific implementations

For example, imagine an Animal class. What sound does a generic "Animal" make? It doesn't make sense to have a plain Animal object—only specific animals like Dog or Cat should exist.

Syntax

public abstract class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
}

If you try new Animal("Bob"), the compiler prevents it with an error. You must create a concrete subclass:

public class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
}

Dog dog = new Dog("Buddy"); // ✓ This works

Key Takeaway: The abstract keyword prevents direct instantiation of a class, forcing developers to create subclasses that provide complete, specific implementations.