![]() |
VOOZH | about |
Method Overriding is a process where a method defined in the base (or parent) class is redefined by a method with the same name and signature in the derived (or child) class. This allows the child class to provide its own implementation of the method, which can either extend or completely replace the behavior defined in the base class.
Method Overriding is particularly useful when we want to alter the behavior of any method inherited from the parent class in the child class.
Implementing Method Overriding in TypeScript
In this example, we declare two classes. Inside the parent class, we declare a method which is then overridden by the child class with its own logic.
Output:
Name: Rohit,
Roll Number: 2,
Scores: 96 out of 100
Rohit scores well...
In this example, we display the result of the parent class method inside the child class's overridden method using the super keyword.
Output:
Name: Rohit,
Roll Number: 2,
Scores: 96 out of 100
Rohit is an intelligent boy..
Rohit scores well...