Code Logo

Abstract Class with Abstract Method

Published at25 Jul 2026
TypeScript Classes & OOP Medium 0 views
Like0

Write a TypeScript abstract class Animal with an abstract method makeSound(): string, and a concrete subclass Dog that implements it. Abstract classes cannot be instantiated directly; they serve as base classes that define a shared interface and common behavior.

The abstract keyword in TypeScript marks a class or method as abstract. Abstract methods have no implementation in the base class and must be overridden in concrete subclasses. Abstract classes can also have concrete methods with shared implementations. This is TypeScript's mechanism for achieving the template method pattern.

Abstract classes differ from interfaces in that they can contain implementation code, constructors, and access modifiers. A class can extend only one abstract class but can implement multiple interfaces. Abstract classes are compiled to JavaScript functions with prototype methods, and the abstract enforcement is only at the type level.

Time complexity is O(1) for method dispatch. Space complexity is O(1) for instances. The abstract keyword is enforced at compile time; attempting to instantiate an abstract class produces a TypeScript compiler error.

Edge cases include abstract classes with multiple abstract methods, abstract classes with concrete methods that call abstract methods, and ensuring the subclass properly calls the parent constructor with super().

Example Input & Output

Example 1
Input
()
Output
This animal says Woof!
Explanation

Consistent output

Example 2
Input
()
Output
This animal says Woof!
Explanation

Final check

Example 3
Input
()
Output
This animal says Woof!
Explanation

Dog says Woof!

Example 4
Input
()
Output
This animal says Woof!
Explanation

Stays same

Example 5
Input
()
Output
This animal says Woof!
Explanation

Same result

Algorithm Flow

Recommendation Algorithm Flow for Abstract Class with Abstract Method
Recommendation Algorithm Flow for Abstract Class with Abstract Method

Solution Approach

abstract class Animal {
  abstract makeSound(): string;
  describe(): string {
    return "This animal says " + this.makeSound();
  }
}

class Dog extends Animal {
  makeSound(): string {
    return "Woof!";
  }
}

function solution(): string {
  var d = new Dog();
  return d.describe();
}

Best Answers

typescript - Approach 1
abstract class Animal {
  abstract makeSound(): string;
  describe(): string {
    return "This animal says " + this.makeSound();
  }
}

class Dog extends Animal {
  makeSound(): string {
    return "Woof!";
  }
}

function solution(): string {
  var d = new Dog();
  return d.describe();
}