An abstract class in Java is a class declared with the abstract keyword that cannot be instantiated. It can contain abstract methods without a body, which subclasses must implement, along with normal methods, fields and constructors that provide shared behaviour.
Use an abstract class when related classes share state and code but each needs its own version of some operations. Since Java 17, an abstract class can also be combined with sealed to control exactly which subclasses are allowed.
Put abstract before class to make the class abstract, and before a method signature (with no body, ending in a semicolon) to make that method abstract. A class with an abstract method must itself be abstract.
speak() is written once in Animal and calls the abstract sound(), which each subclass defines. This is the template method pattern.
An abstract class is more flexible than an interface because it can hold state.
new on the abstract type, but a subclass calls them with super(...).private, and not final or static, since those would make them impossible to override.Constructors initialise fields that every subclass shares. The subclass constructor must call one of them, directly with super(...) or implicitly when a no-argument constructor exists.
A caution: do not call an overridable method from an abstract class constructor. The subclass fields are not initialised yet, so the method sees default values.
| Feature | Abstract class | Interface |
|---|---|---|
| Instance fields | Yes | No (only constants) |
| Constructors | Yes | No |
| Method bodies | Yes | default, static and private methods |
| Inheritance | Extend one class | Implement many interfaces |
| Access modifiers on methods | Any | Public (plus private helpers) |
| Best for | Shared state and partial implementation | A capability or contract |
Prefer an interface when you only need a contract, because a class can implement many of them. Choose an abstract class when subclasses share fields or constructor logic.
From Java 17 you can restrict who may extend an abstract class with sealed and permits. Combined with pattern matching for switch (final in Java 21), the compiler can check that a switch covers every subclass.
No default branch is needed because the hierarchy is closed, and adding a new permitted subclass makes the switch fail to compile until you handle it. If the subclasses need no shared state, a sealed interface with records is often a lighter choice.
new. Create a subclass or an anonymous subclass instead.private, static or final. These combinations are compile errors.Yes. Declaring it abstract still blocks direct instantiation, which is useful for a base class that should only be extended.
Yes. The abstract class can leave interface methods unimplemented, and the first concrete subclass must implement them.
No. final forbids subclassing and abstract requires it, so the compiler rejects the combination.
Yes, new Animal("x") { String sound() { return "..."; } } creates an anonymous subclass. You are instantiating that subclass, not the abstract class itself.
An abstract class declares methods that subclasses must implement while sharing fields, constructors and concrete methods. Choose it for shared state, prefer interfaces for pure contracts and use sealed on Java 17 and later when you want a closed hierarchy that switch can check exhaustively.