Abstract class
Абстрактный класс
An abstract class is a base class you cannot instantiate directly. It can provide shared code and declare operations that concrete subclasses must implement.
What an abstract class does
Suppose several shipping options calculate prices differently, but all must reject packages with an invalid weight. An abstract base class can handle validation once and let each shipping option supply its own pricing formula. Code that needs a quote can work with the base type without knowing which option it received.
How it works
In Java, the abstract modifier stops callers from creating an instance of the class directly. The class can still have fields, constructors, and methods with working implementations. It can also declare abstract methods, which have a signature but no body. A concrete subclass must have an implementation for every inherited abstract method, either by defining it or by inheriting an implementation. An abstract subclass can leave some of that work to its own subclasses.
The restriction is on creating objects, not on using the class as a type. A variable declared with an abstract type can hold a reference to a concrete subclass object. When an overridden method is called, the implementation is chosen based on the actual object. This is a form of polymorphism.
Java example
abstract class ShippingOption {
public final long quoteCents(int grams) {
if (grams <= 0) {
throw new IllegalArgumentException("Weight must be positive");
}
return calculateCents(grams);
}
protected abstract long calculateCents(int grams);
}
class FlatRateShipping extends ShippingOption {
@Override
protected long calculateCents(int grams) {
return 599;
}
}
ShippingOption option = new FlatRateShipping(); is valid; new ShippingOption() is not. The call option.quoteCents(500) runs the base class's validation and then calls the flat-rate implementation, returning 599. Marking quoteCents as final prevents subclasses from overriding it and skipping validation. This structure is an example of the Template Method pattern: the base class controls the sequence, and subclasses supply individual steps.
Abstract class, interface, or composition?
Choose an abstract class when closely related types need shared instance state or a common algorithm with a few customizable steps. If unrelated types simply need to offer the same operations, an interface is often a better fit. Java interfaces can have default methods, so “interfaces never contain code” is not a useful distinction. Unlike a class, a Java interface cannot store per-object state in instance fields, and a class can implement multiple interfaces but extend only one class directly. If two classes merely share a helper, give them that helper as a separate object instead of making them inherit from the same base class.
Interview questions
Can an abstract class have a constructor? Yes. The base constructor runs during construction of a subclass instance and can initialize inherited state. Must the class have an abstract method? Not in Java: you can mark a class abstract solely to prevent callers from instantiating it directly.