0
0
Typescriptprogramming~10 mins

Abstract classes in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract classes
Define abstract class with abstract methods
Create subclass that extends abstract class
Implement all abstract methods in subclass
Create instance of subclass
Call implemented methods on instance
Program runs using subclass behavior
Abstract classes define methods without implementation. Subclasses must implement these methods before creating instances.
Execution Sample
Typescript
abstract class Animal {
  abstract makeSound(): void;
  move(): void {
    console.log('Moving...');
  }
}

class Dog extends Animal {
  makeSound(): void {
    console.log('Bark');
  }
}

const dog = new Dog();
dog.makeSound();
dog.move();
Defines an abstract class Animal with an abstract method. Dog subclass implements the method and instances can be created.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method makeSound and concrete method moveClass Animal createdNo instance can be made of Animal
2Define class Dog extending AnimalDog must implement makeSoundDog class created
3Implement makeSound in DogDog.makeSound() logs 'Bark'Method implemented
4Create instance dog of Dogdog is instance of Dogdog created
5Call dog.makeSound()Calls Dog's makeSoundOutput: Bark
6Call dog.move()Calls Animal's moveOutput: Moving...
7Try to create instance of AnimalError: Cannot instantiate abstract classInstantiation fails
💡 Abstract class Animal cannot be instantiated directly; subclass Dog implements abstract methods allowing instantiation.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
dogundefinedDog instance createddog.makeSound() calleddog.move() calleddog instance exists
Key Moments - 3 Insights
Why can't we create an instance of the abstract class Animal?
Because Animal has an abstract method without implementation, TypeScript prevents creating instances until all abstract methods are implemented, as shown in step 7 of the execution_table.
What happens if Dog does not implement the abstract method makeSound?
TypeScript will give an error and Dog cannot be instantiated. This is implied in step 2 and 3 where Dog must implement makeSound before instances can be created.
Can the subclass Dog use concrete methods from Animal?
Yes, Dog inherits concrete methods like move from Animal, as shown in step 6 where dog.move() calls Animal's method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 7?
AError because Animal is abstract and cannot be instantiated
BAn instance of Animal is created successfully
CDog instance is created
DmakeSound method is called
💡 Hint
Check the 'Action' and 'Result' columns at step 7 in execution_table
At which step is the dog instance created?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'Create instance dog of Dog' in the execution_table
If Dog did not implement makeSound, what would happen?
ADog could still be instantiated
BmakeSound would default to Animal's implementation
CTypeScript would give an error preventing instantiation
Ddog.move() would fail
💡 Hint
Refer to key_moments about subclass implementation requirements
Concept Snapshot
abstract class ClassName {
  abstract methodName(): returnType; // no body
  concreteMethod() { ... } // optional
}

class Subclass extends ClassName {
  methodName() { ... } // must implement all abstract methods
}

Cannot create instances of abstract classes directly.
Subclasses must implement all abstract methods before instantiation.
Full Transcript
An abstract class in TypeScript is a class that cannot be instantiated directly because it contains one or more abstract methods without implementation. These abstract methods act like placeholders that subclasses must implement. In the example, Animal is an abstract class with an abstract method makeSound and a concrete method move. The Dog class extends Animal and implements makeSound. Only after implementing all abstract methods can we create an instance of Dog. Calling dog.makeSound() outputs 'Bark' and dog.move() outputs 'Moving...'. Trying to create an instance of Animal directly causes an error. This ensures that abstract classes define a contract for subclasses to follow.