TypeScript – Method Overriding

Method Overriding is a process of overthrowing a method of super class by method of same name and parameters in sub class.

Method Overriding is useful when sub class wants to modify the behavior of super class for certain tasks.

Example

Following is a simple example of method overriding where eat() method of Student class overrides the eat() method of Person class.

When the above .ts program is compiled to .js file.

You can execute .js file using a try javascript online editor by copying the javascript code to the code editor,

Call Super Class Methods from Sub-Class

Immediate super class methods could be called from sub class using super keyword.

Replace this method with that of in Student class and run the program.

The eat() method in Person class is executed for super.eat().

Conclusion

In this TypeScript Tutorial, we have learnt to override the behavior of parent classes using Method Overriding with the help of Example TypeScript program.