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".

GroupFunctions & Classes
KindSyntax
Strict modeNot 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 classAbstract class: cannot be new-ed, only extended
abstract area(): numberAbstract member: signature only; subclasses must implement it or the compiler errors
protectedOften 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏