0
0
Typescriptprogramming~10 mins

Abstract methods in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract methods
Define abstract class
Declare abstract method (no body)
Create subclass
Implement abstract method in subclass
Instantiate subclass and call method
Method runs with subclass implementation
Abstract methods are declared without a body in an abstract class and must be implemented in subclasses before use.
Execution Sample
Typescript
abstract class Animal {
  abstract makeSound(): void;
}

class Dog extends Animal {
  makeSound() { console.log('Woof!'); }
}

const dog = new Dog();
dog.makeSound();
This code defines an abstract method makeSound in Animal, implements it in Dog, then calls it.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method makeSoundNo method bodyAnimal cannot be instantiated
2Define class Dog extending AnimalDog must implement makeSoundDog class created
3Implement makeSound in DogMethod prints 'Woof!'Dog.makeSound ready
4Create instance dog of Dogdog is an object of Dogdog created
5Call dog.makeSound()Runs Dog's makeSoundOutput: Woof!
6Try to instantiate AnimalError: Cannot create instance of abstract classInstantiation fails
💡 Execution stops after dog.makeSound() prints 'Woof!' and abstract class cannot be instantiated
Variable Tracker
VariableStartAfter Step 4After Step 5Final
Animalabstract class definedunchangedunchangedunchanged
Dogclass definedunchangedunchangedunchanged
dogundefinedinstance of Doginstance of Doginstance of Dog
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 instantiation to ensure subclasses provide the method (see execution_table step 6).
What happens if Dog does not implement the abstract method makeSound?
TypeScript will give an error because Dog must implement all abstract methods from Animal (see execution_table step 2).
When dog.makeSound() is called, which method runs?
The implementation in Dog runs, printing 'Woof!' (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 6?
AAn instance of Animal is created successfully
BError because Animal is abstract and cannot be instantiated
CDog's makeSound method is called
DNothing happens
💡 Hint
Check the 'Action' and 'Result' columns at step 6 in the execution_table
At which step is the dog object created?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where 'Create instance dog of Dog' happens in execution_table
If Dog did not implement makeSound, what would happen?
ATypeScript would give a compile-time error
BDog would compile but throw error at runtime
CDog would be abstract too
DNothing, code runs fine
💡 Hint
Refer to key_moments about implementation requirements and execution_table step 2
Concept Snapshot
abstract class ClassName {
  abstract methodName(): returnType;
}

- Abstract methods have no body.
- Subclasses must implement all abstract methods.
- Abstract classes cannot be instantiated directly.
- Calling abstract methods runs subclass implementation.
Full Transcript
Abstract methods in TypeScript are declared inside abstract classes without a method body. These methods must be implemented by any subclass. The abstract class itself cannot be instantiated. In the example, Animal is an abstract class with an abstract method makeSound. Dog extends Animal and implements makeSound by printing 'Woof!'. We create an instance of Dog and call makeSound, which outputs 'Woof!'. Trying to instantiate Animal directly causes an error. This ensures that abstract methods enforce a contract for subclasses to provide specific behavior.