Abstract classes

Abstract skeletons

An abstract class holds shared code that is not meant to be used alone. It cannot be created with new, only extended by children.

abstract class Shape { label(): string { return "A shape"; } } class Point extends Shape {} console.log(new Point().label());

A shape

Abstract methods

An abstract method declares a signature without a body. Every child must provide its own version of that method.

abstract class Shape { abstract area(): number; } class Square extends Shape { constructor(public side: number) { super(); } area(): number { return this.side * this.side; } } console.log(new Square(3).area());

9

Extend them

Extend the abstract class to build a concrete child. Call super in the child constructor, then fill in each abstract method.

abstract class Shape { abstract area(): number; } class Circle extends Shape { constructor(public radius: number) { super(); } area(): number { return 3.14 * this.radius * this.radius; } } const circle = new Circle(2); console.log(circle.area());

12.56

Abstract vs interface

Use an abstract class when children share real code, like helpers and default logic. Use an interface when only the shape matters and each class brings its own logic.

A class extends one parent but can implement many interfaces, so interfaces stay the tool for wide contracts.

interface HasArea { area(): number; } class Square implements HasArea { constructor(public side: number) {} area(): number { return this.side * this.side; } } console.log(new Square(3).area());

9

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What can be done with an abstract class?

    Answer

    It can be extended, but not created with new

    The class is a skeleton for children, so the compiler rejects a direct instance.

  2. What is an abstract method?

    Answer

    A signature with no body that every child must provide

    The declaration states the contract, and a concrete child fills in the implementation.

  3. When is an abstract class a better fit than an interface?

    Answer

    When children share real code such as helpers and default logic

    The abstract class can carry implemented methods, while an interface carries only the shape.

  4. What does this program print? abstract class Shape { label(): string { return "A shape"; } } class Point extends Shape {} console.log(new Point().label());

    Answer

    A shape

    The child inherits the implemented method, and creating the child is allowed.

  5. What does this program print? abstract class Shape { abstract area(): number; } class Square extends Shape { constructor(public side: number) { super(); } area(): number { return this.side * this.side; } } console.log(new Square(3).area());

    Answer

    9

    The child provides the method, which multiplies the side by itself.

  6. What does this program print? abstract class Shape { abstract area(): number; } class Circle extends Shape { constructor(public radius: number) { super(); } area(): number { return 3.14 * this.radius * this.radius; } } const circle = new Circle(2); console.log(circle.area());

    Answer

    12.56

    The area is 3.14 times two squared, which is 12.56.

  7. What does this program print? interface HasArea { area(): number; } class Square implements HasArea { constructor(public side: number) {} area(): number { return this.side * this.side; } } console.log(new Square(3).area());

    Answer

    9

    The class provides the method the interface names, and the call returns nine.

  8. What does the compiler say about this program? abstract class Shape { label(): string { return "A shape"; } } console.log(new Shape().label());

    Answer

    error TS2511: Cannot create an instance of an abstract class.

    An abstract class has children to create it, so a direct new is a type error.

  9. What does the compiler say about this program? abstract class Shape { abstract area(): number; } class Circle extends Shape { } console.log(new Circle());

    Answer

    error TS2515: Non-abstract class 'Circle' does not implement inherited abstract member area from class 'Shape'.

    A concrete child must implement every abstract member it inherits.

  10. How many parents can a class extend, and how many interfaces can it implement?

    Answer

    One parent and many interfaces

    A class has a single parent, which is why wide contracts are written as interfaces.

Article author: Arthur Isaev