abstract class Abstract Classes
Last updated: 2026-10-22
abstract class Abstract Classes
An abstract class is a half-finished base class: it can carry real implementation while declaring abstract members (abstract area(): number) that subclasses must fill in. It cannot be instantiated directly, only extended — ideal for "subclasses share the skeleton but own the details".
| Group | Functions & Classes |
|---|---|
| Kind | Syntax |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
abstract class Shape {
constructor(protected name: string) {}
abstract area(): number; // abstract: signature only
describe(): string { // concrete: inherited as-is
return `${this.name}: ${this.area().toFixed(2)}`;
}
}
class Circle extends Shape {
constructor(private radius: number) { super("circle"); }
area(): number { return Math.PI * this.radius ** 2; }
}
⚙️ Parameters
| abstract class | Abstract class: cannot be new-ed, only extended |
|---|---|
| abstract area(): number | Abstract member: signature only; subclasses must implement it or the compiler errors |
| protected | Often paired with abstract classes: state like name meant for subclasses |
Returns:Abstract methods declare signatures only; concrete methods get normal return types. No abstract instance ever exists at runtime — every object is a subclass instance.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
circle: 3.14
square: 4.00
(Runtime output after transpilation; new Shape("x") is compile error TS2511 — even though the emitted JS is just an ordinary class)
💡 Notes
ℹ️ Info Abstractness is compile-time too: tsc forbids new Shape(), but the emitted JS is a plain class and new would succeed at runtime — it is a guardrail, not a wall.
💡 Tip Abstract class vs interface: abstract classes carry state and implementation (the heart of the template-method pattern); interfaces describe shapes only. Share code → abstract class; constrain shape → interface. You can use both together.
⚠️ Note A class can extend only one base; for multiple capabilities use interfaces plus composition rather than forcing a single inheritance chain.
🏷️ Related Functions & Classes
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q What happens if you new an abstract class?
A Compile error TS2511 (cannot create an instance of an abstract class). The emitted JS is a plain class and new would succeed at runtime — never rely on runtime protection.
Q Can an abstract class have a constructor?
A Yes, often combined with protected: a protected constructor also blocks external new (subclasses may still call super) — another way to forbid direct instantiation.
Q Abstract class or interface?
A Sharing implementation/state (a describe method, a name field) → abstract class; a pure contract, implementable many times → interface. A common combo: interface for the public contract, abstract class for the default implementation.