Inheritance in TypeScript

We just saw how to implement an inheritance in JavaScript using a prototype. Now, we will see how an inheritance can be implemented in TypeScript.

In TypeScript, similar to extending interfaces, we can also extend a class by inheriting another class, as illustrated:

class SimpleCalculator { 
z: number;
constructor() { }
addition(x: number, y: number) {
z = x + y;
}
subtraction(x: number, y: number) {
z = x - y;
}
}
class ComplexCalculator extends SimpleCalculator {
constructor() { super(); }
multiplication(x: number, y: number) {
z = x * y;
}
division(x: number, y: number) {
z = x / y;
}
}
var calculator = new ComplexCalculator();
calculator.addition(10, 20);
calculator.Substraction(20, 10);
calculator.multiplication(10, 20);
calculator.division(20, 10);

Here, we are able to access the methods of SimpleCalculator using the instance of ComplexCalculator as it extends SimpleCalculator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset