What is an abstract class in Java, and what can it contain that a regular interface (historically) could not?
answer
- abstract keyword, cannot be instantiated
- can hold abstract + concrete methods
- can hold mutable instance fields + constructors
- one abstract method forces class abstract
- extended, not new'd
basics
~20 sAn abstract class is a class marked with the abstract keyword that you cannot create objects from directly. It can mix finished methods with unfinished (abstract) methods that subclasses must complete. It can also hold normal fields and constructors.
solid answer
~40 sAn abstract class is declared with the abstract keyword and cannot be instantiated on its own; you must subclass it and create the subclass instead. Its purpose is to serve as a partially-built base: it can declare abstract methods (no body, ending in a semicolon) that force subclasses to provide an implementation, while also providing concrete methods, instance and static fields, constructors, and any access modifiers. This lets you share state and common code in the base class while leaving the parts that differ per subclass abstract. A class with even one abstract method must itself be abstract. Abstract classes are how Java expresses the 'abstraction' OOP pillar alongside interfaces, the difference being that an abstract class can carry mutable instance state and a single-inheritance relationship.
code
java · 17 linespublic abstract class Shape {
private final String name; // mutable-capable instance state
protected Shape(String name) { // constructor: runs via super(...)
this.name = name;
}
public String name() { return name; } // concrete method, inherited
public abstract double area(); // abstract: subclass must implement
}
class Circle extends Shape {
private final double r;
Circle(double r) { super("circle"); this.r = r; }
@Override public double area() { return Math.PI * r * r; }
}
// new Shape("x"); // COMPILE ERROR: cannot instantiate abstract Shape
Shape s = new Circle(2); // OK: instantiate a concrete subclassgo deeper
Can define abstract class, state the keyword, and that you can't do new on it — you subclass instead.
Explains the mix of abstract + concrete members, the 'one abstract method forces abstract class' rule, and that abstract classes hold mutable state and constructors.
Frames it against interfaces (mutable state, single inheritance, constructor role) and explains when to reach for an abstract base in design.
Discusses skeletal-implementation design (Template Method), the inheritance-coupling cost, and why composition or sealed types may be preferable; reasons about API evolution constraints of exposing an abstract base.
## What 'abstract' means In object-oriented programming, **abstraction** means exposing *what* something does while hiding or deferring *how* it does it. Java offers two tools for this: **interfaces** and **abstract classes**. This leaf is about abstract classes. A **class** in Java is a blueprint for objects: it declares **fields** (data each object holds) and **methods** (behavior). Normally you create an object from a class with the `new` keyword, e.g. `new ArrayList()`. This act of creating an object is called **instantiation**. An **abstract class** is a class you mark with the `abstract` keyword: ```java public abstract class Shape { } ``` The defining rule: **you cannot instantiate an abstract class directly.** Writing `new Shape()` is a compile error. The class exists only to be **extended** (subclassed) — another class uses `extends Shape` and *that* subclass is what you instantiate. ## What it can contain An abstract class is a *real class* in every other respect. It can contain: - **Abstract methods** — a method declared with `abstract` and **no body**, ending in a semicolon: `public abstract double area();`. This says 'every concrete subclass must supply this.' - **Concrete (implemented) methods** — ordinary methods with bodies. Subclasses inherit them for free. - **Instance fields** — including *mutable* state, which subclasses inherit. This is a key difference from interfaces, which can only hold constants (`public static final`). - **Constructors** — even though you can't call `new Shape()`, the constructor still runs, invoked by the subclass constructor via `super(...)` to initialize the inherited fields. - **Static members**, any **access modifier** (private, protected, etc.), and nested types. ## The 'one abstract method ⇒ class must be abstract' rule If a class declares *any* abstract method, the class **must** itself be declared abstract — otherwise it would have an unimplemented method but still be instantiable, which makes no sense. The reverse is *not* required: an abstract class is allowed to have **zero** abstract methods (you'd do this purely to prevent direct instantiation while still sharing code). ## Why use one Use an abstract class when several classes share **common state and behavior** but differ in specific steps. The base class implements the shared parts once and leaves the varying parts abstract. Subclasses fill in only what differs. This avoids code duplication and encodes an 'IS-A' relationship (a `Circle` IS-A `Shape`). ## Abstract vs interface (historical note) Historically (before Java 8), an interface could contain *only* abstract method signatures and constants — no implemented methods and no instance fields. An abstract class could always carry concrete methods **and mutable instance fields**, so it was the only way to share implementation up a hierarchy. Java 8+ added `default` and `static` methods to interfaces, narrowing the gap — but interfaces *still* cannot hold mutable instance state, and a class can extend only one (abstract) class while implementing many interfaces. So the two remain distinct tools.
- Can an abstract class have no abstract methods at all?Yes. It's legal and sometimes useful purely to prevent direct instantiation while still providing shared implemented methods and fields. The only hard rule is the converse: any class with an abstract method must be declared abstract.
- If abstract classes can't be instantiated, why do they have constructors?The constructor initializes the abstract class's own fields. It can't be called via new, but it is invoked by a subclass constructor through super(...), so the inherited fields get set up before the subclass body runs.
saying these in an interview costs you the question
- Saying an abstract class cannot have constructors (it can — they run during subclass construction).
- Claiming an abstract class must have at least one abstract method (it can have zero).
- Confusing 'cannot be instantiated' with 'has no constructor'.
- Saying abstract classes can't hold state — they can hold mutable instance fields, unlike interfaces.