Menu

Java Abstract Classes: abstract Keyword Explained

What a Java abstract class is, how to declare abstract methods, why you can't instantiate one, and when to choose an abstract class over an interface.

This page includes runnable editors - edit, run, and see output instantly.

What an Abstract Class Is

An interface declares behavior with no state. A regular class is fully implemented and can be instantiated. An abstract class sits between the two: it can carry fields, constructors, and finished methods like a normal class, but it can also leave some methods unimplemented and forbids being instantiated directly. You mark it with the abstract keyword.

The idea is to capture everything subclasses have in common in one place, while forcing each subclass to fill in the parts that genuinely differ.

Animal defines getName() once for every subclass and declares sound() as abstract - a method with a signature but no body that Dog must supply.

You Cannot Instantiate an Abstract Class

Because an abstract class may have unfinished methods, creating one directly would leave you with an incomplete object. The compiler rejects it:

Animal a = new Animal("???");   // error: Animal is abstract; cannot be instantiated

You always instantiate a concrete subclass - one that has implemented every abstract method. That subclass instance can then be held in a variable of the abstract type, which is exactly how you use the abstraction.

Abstract Methods Force Subclasses to Decide

An abstract method is a promise the subclass must keep. If a subclass forgets to implement one, the subclass itself becomes abstract and the compiler tells you so. This is the abstract class's main lever: it guarantees that certain behavior exists without dictating what it does.

describe() is written once in Shape yet calls each subclass's own area(). The abstract class supplies the common scaffolding; the subclasses supply the specifics.

Shared State and Constructors

Unlike a traditional interface, an abstract class can hold instance fields and define constructors. The constructor never makes an Animal or Shape by itself - it runs via super(...) when a subclass is created, initializing the shared state.

The balance field, deposit, and applyInterest live in one place. Only the policy that actually varies - interestRate() - is left abstract. A subclass's constructor must call super(...) to initialize that inherited state.

A Gotcha: Mixing abstract and final

abstract and final are opposites. An abstract method demands an override; a final method forbids one. Marking the same thing both ways - or making a final abstract class - is a compile error. Also remember an abstract class can have zero abstract methods: declaring a class abstract purely to stop it being instantiated is legal and sometimes useful for base types you only ever want to extend.

abstract final class Bad { }        // error: abstract and final conflict

abstract class Base {
    abstract final void f();        // error: an abstract method can't be final
}

Abstract Class vs Interface

They overlap, so the choice comes down to what you need to share:

  • Abstract class - use it for closely related classes that share state and code. A Savings and a Checking both extend Account, inheriting the balance field and the deposit logic. A class extends only one.
  • Interface - use it for a capability unrelated classes can share. A Bird and an Airplane can both be Flyable without sharing any implementation. A class can implement many.

A frequent pattern combines them: an interface defines the contract, and an abstract class implements the boilerplate so concrete subclasses fill in only what is unique.

Next: Polymorphism

Notice that in every example above we held a subclass instance in a variable of the abstract type, then called a method and got the subclass's behavior automatically. That single ability - one reference type, many runtime behaviors - is polymorphism, and it's what makes abstract classes and interfaces pay off. That is the next page.

Frequently Asked Questions

What is an abstract class in Java?

An abstract class is a class declared with the abstract keyword that cannot be instantiated on its own. It is meant to be extended. It can mix fully implemented methods and fields (shared state and code for subclasses) with abstract methods that have no body - those become the responsibility of each subclass to implement.

Can you instantiate an abstract class in Java?

No. new AbstractType() is a compile error because an abstract class may have unimplemented (abstract) methods, so the object would be incomplete. You instantiate a concrete subclass that fills in all the abstract methods, then store it in a variable of the abstract type.

What is the difference between an abstract class and an interface in Java?

An abstract class can have instance fields, constructors, and partially implemented logic, but a class can extend only one. An interface declares behavior with no instance state, and a class can implement many. Use an abstract class to share state and code among closely related subclasses; use an interface to give unrelated classes a common capability.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED