TypeScript Class Inheritance and Polymorphism
\\n\\nTypeScript supports the inheritance and polymorphism features of object-oriented programming.
\\n\\n\\n\\n
Class Inheritance
\\n\\nUse the extends keyword to implement inheritance.
Example
\\n\\nclass Animal {\\n\\n name: string;\\n\\nconstructor(name: string){\\n\\nthis.name= name;\\n\\n}\\n\\nspeak():void{\\n\\n console.log(this.name+" make a sound");\\n\\n}\\n\\n}\\n\\nclass Dog extends Animal {\\n\\n breed: string;\\n\\nconstructor(name: string, breed: string){\\n\\nsuper(name);// Call parent class constructor\\n\\nthis.breed= breed;\\n\\n}\\n\\nspeak():void{\\n\\n console.log(this.name+" Woof woof woof!");\\n\\n}\\n\\n}\\n\\nvar dog =new Dog("Wangcai","Golden Retriever");\\n\\n dog.speak();\\n\\n\\n\\nOutput:
\\n\\nWangcai Woof woof woof!
\\n\\n\\n\\n
The super Keyword
\\n\\nsuper is used to call methods and constructors of the parent class.
Example
\\n\\nclass Shape {\\n\\n color: string;\\n\\nconstructor(color: string){\\n\\nthis.color= color;\\n\\n}\\n\\ndescribe(): string {\\n\\nreturn"This is a "+this.color+" 's shape";\\n\\n}\\n\\n}\\n\\nclass Circle extends Shape {\\n\\n radius: number;\\n\\nconstructor(color: string, radius: number){\\n\\nsuper(color);\\n\\nthis.radius= radius;\\n\\n}\\n\\n// Override parent class method\\n\\n describe(): string {\\n\\n// Call parent class method and extend\\n\\nreturn super.describe()+"οΌradius is "+this.radius;\\n\\n}\\n\\narea(): number {\\n\\nreturn Math.PI*this.radius*this.radius;\\n\\n}\\n\\n}\\n\\nvar circle =new Circle("Red",5);\\n\\n console.log(circle.describe());\\n\\n console.log("Area: "+ circle.area().toFixed(2));\\n\\n\\n\\nOutput:
\\n\\nThis is a Red's shapeοΌradius is 5Area: 78.54
\\n\\n\\n\\n
Polymorphism
\\n\\nInstances of subclasses can be assigned to variables of the parent class type.
\\n\\nExample
\\n\\nclass Animal {\\n\\n name: string;\\n\\n constructor(name: string){this.name= name;}\\n\\n speak():void{\\n\\n console.log(this.name+" make a sound");\\n\\n}\\n\\n}\\n\\nclass Cat extends Animal {\\n\\n speak():void{\\n\\n console.log(this.name+" Meow meow meow!");\\n\\n}\\n\\n}\\n\\nclass Dog extends Animal {\\n\\n speak():void{\\n\\n console.log(this.name+" Woof woof woof!");\\n\\n}\\n\\n}\\n\\n// Polymorphism: Storing instances of different subclasses in an array\\n\\nvar animals: Animal[]=[\\n\\nnew Cat("Xiaobai"),\\n\\nnew Dog("Wangcai"),\\n\\nnew Animal("Animal")\\n\\n];\\n\\n// Calling the same method yields different implementations across subclasses\\n\\nfor(var _i =0, animals_1 = animals; _i < animals_1.length; _i++){\\n\\nvar animal = animals_1;\\n\\n animal.speak();\\n\\n}\\n\\n\\n\\nOutput:
\\n\\nXiaobai Meow meow meow!Wangcai Woof woof woof!Animal make a sound
\\n\\n\\n\\n
The instanceof Check
\\n\\nUse instanceof to check the type of an instance.
Example
\\n\\nclass Rectangle {\\n\\n width: number;\\n\\n height: number;\\n\\n constructor(width: number, height: number){\\n\\nthis.width= width;\\n\\nthis.height= height;\\n\\n}\\n\\n area(): number {\\n\\nreturn this.width*this.height;\\n\\n}\\n\\n}\\n\\nclass Circle {\\n\\n radius: number;\\n\\n constructor(radius: number){\\n\\nthis.radius= radius;\\n\\n}\\n\\n area(): number {\\n\\nreturn Math.PI*this.radius**2;\\n\\n}\\n\\n}\\n\\nvar shapes =[new Rectangle(4,5),new Circle(3)];\\n\\nfor(var _i =0, shapes_1 = shapes; _i < shapes_1.length; _i++){\\n\\nvar shape = shapes_1;\\n\\nif(shape instanceof Rectangle){\\n\\n console.log("Rectangle Area: "+ shape.area());\\n\\n}else if(shape instanceof Circle){\\n\\n console.log("Circle Area: "+ shape.area().toFixed(2));\\n\\n}\\n\\n}\\n\\n\\n\\nOutput:
\\n\\nRectangle Area: 20Circle Area: 28.27
\\n\\n\\n\\n
protected Members
\\n\\nprotected members are accessible within subclasses.
Example
\\n\\nclass Person {\\n\\n protected name: string;\\n\\nconstructor(name: string){\\n\\nthis.name= name;\\n\\n}\\n\\n}\\n\\nclass Employee extends Person {\\n\\n private department: string;\\n\\nconstructor(name: string, department: string){\\n\\nsuper(name);\\n\\nthis.department= department;\\n\\n}\\n\\npublic introduce(): string {\\n\\n// Can access protected members\\n\\nreturn"I am "+this.name+"οΌin "+this.department+" work";\\n\\n}\\n\\n}\\n\\nvar emp =new Employee("Alice","Technical Department");\\n\\n console.log(emp.introduce());\\n\\n// console.log(emp.name); // Error: Protected members cannot be accessed externally\\n\\n\\n\\nOutput:
\\n\\nI am AliceοΌin Technical Department work
\\n\\n\\n\\n
Summary
\\n\\n- \\n
- Inheritance: Use the
extendskeyword \\n super: Call the parent class \\n- Polymorphism: Same interface, different implementations \\n
protected: Accessible in subclasses \\n
YouTip