YouTip LogoYouTip

Ts Class Inheritance

TypeScript Class Inheritance and Polymorphism

\\n\\n

TypeScript supports the inheritance and polymorphism features of object-oriented programming.

\\n\\n
\\n\\n

Class Inheritance

\\n\\n

Use the extends keyword to implement inheritance.

\\n\\n

Example

\\n\\n
class 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\\n

Output:

\\n\\n

Wangcai Woof woof woof!

\\n\\n
\\n\\n

The super Keyword

\\n\\n

super is used to call methods and constructors of the parent class.

\\n\\n

Example

\\n\\n
class 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\\n

Output:

\\n\\n

This is a Red's shape,radius is 5Area: 78.54

\\n\\n
\\n\\n

Polymorphism

\\n\\n

Instances of subclasses can be assigned to variables of the parent class type.

\\n\\n

Example

\\n\\n
class 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\\n

Output:

\\n\\n

Xiaobai Meow meow meow!Wangcai Woof woof woof!Animal make a sound

\\n\\n
\\n\\n

The instanceof Check

\\n\\n

Use instanceof to check the type of an instance.

\\n\\n

Example

\\n\\n
class 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\\n

Output:

\\n\\n

Rectangle Area: 20Circle Area: 28.27

\\n\\n
\\n\\n

protected Members

\\n\\n

protected members are accessible within subclasses.

\\n\\n

Example

\\n\\n
class 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\\n

Output:

\\n\\n

I am Alice,in Technical Department work

\\n\\n
\\n\\n

Summary

\\n\\n
    \\n
  • Inheritance: Use the extends keyword
  • \\n
  • super: Call the parent class
  • \\n
  • Polymorphism: Same interface, different implementations
  • \\n
  • protected: Accessible in subclasses
  • \\n
← Ts Intersection TypesTs Conditional Types β†’