Learn how to override methods in TypeScript classes and use the super keyword to call base class methods and constructors.
In this section, we will delve into the concepts of method overriding and the use of the super
keyword in TypeScript. These are fundamental aspects of object-oriented programming (OOP) that allow us to create flexible and reusable code. By the end of this section, you’ll understand how to override methods in derived classes and how to use super
to call methods and constructors from a base class.
Method overriding occurs when a derived class provides a specific implementation for a method that is already defined in its base class. This allows the derived class to modify or extend the behavior of the base class method.
Let’s start with a simple example. Suppose we have a base class Animal
with a method makeSound
. We’ll create a derived class Dog
that overrides this method.
class Animal {
makeSound(): void {
console.log("Some generic animal sound");
}
}
class Dog extends Animal {
makeSound(): void {
console.log("Bark");
}
}
const myDog = new Dog();
myDog.makeSound(); // Output: Bark
In this example, the Dog
class overrides the makeSound
method of the Animal
class. When we call makeSound
on an instance of Dog
, it executes the overridden method in the Dog
class.
super
KeywordThe super
keyword is used to access and call functions on an object’s parent. It is essential in two main scenarios:
super
.super.methodName()
.super
in ConstructorsWhen you define a constructor in a derived class, you must call super()
before using this
. This ensures that the base class is properly initialized.
class Animal {
constructor(public name: string) {}
}
class Dog extends Animal {
constructor(name: string, public breed: string) {
super(name); // Call the base class constructor
}
}
const myDog = new Dog("Buddy", "Golden Retriever");
console.log(myDog.name); // Output: Buddy
console.log(myDog.breed); // Output: Golden Retriever
In this example, the Dog
class constructor calls super(name)
to pass the name
parameter to the Animal
class constructor.
super
in Method OverridingYou can also use super
to call a method from the base class within an overridden method.
class Animal {
makeSound(): void {
console.log("Some generic animal sound");
}
}
class Dog extends Animal {
makeSound(): void {
super.makeSound(); // Call the base class method
console.log("Bark");
}
}
const myDog = new Dog();
myDog.makeSound();
// Output:
// Some generic animal sound
// Bark
Here, the Dog
class calls super.makeSound()
to execute the makeSound
method from the Animal
class before adding its own behavior.
Forgetting to Call super()
: If you define a constructor in a derived class, you must call super()
before accessing this
. Failing to do so will result in a runtime error.
Incorrect Method Signature: Ensure that the method signature in the derived class matches the one in the base class. Otherwise, you might not be overriding the method but rather defining a new one.
Overusing super
: While super
is powerful, overusing it can lead to tightly coupled code. Use it judiciously to maintain flexibility.
To better understand the flow of method calls in inheritance, let’s visualize it using a diagram.
classDiagram class Animal { +makeSound() } class Dog { +makeSound() } Animal <|-- Dog Animal : makeSound() Dog : makeSound() Dog : super.makeSound()
In this diagram, the Dog
class inherits from the Animal
class and overrides the makeSound
method. The super.makeSound()
call is depicted to show the invocation of the base class method.
Now that we’ve covered the basics, try modifying the examples:
Cat
, that overrides makeSound
to print “Meow”.Animal
class that returns the type of sound the animal makes. Override it in Dog
and Cat
to return specific sounds.super
in different scenarios to see how it affects method behavior.In this section, we’ve explored method overriding and the use of the super
keyword in TypeScript. These concepts are crucial for building flexible and reusable code in an object-oriented style. By understanding how to override methods and call base class constructors and methods, you can create more sophisticated and maintainable applications.