CodesJava

Easy learning with example program codes

typescript class inheritance tutorial


TypeScript Class:

As we discussed in earlier tutorials that TypeScript supports object-oriented programming concepts like classes, interfaces, etc. Class is act as a blue print or template for creating objects. It provides state and behaviour for its objects. A class is used to encapsulate the data for the object.

TypeScript Inheritance:

Inheritance is a way to implement IS-A relationship i.e. parent child relationship. Subclass inherits the super class properties like data member, methods. Inheritance is the way of re-usability of code. Let us take the example of parent and child. A child inherits the properties of its parent.

Why inheritance is used?

  • Code re-usability.
  • Run-time polymorphism.

Syntax:

class subclass extends superclass{
   //subclass code
}

Example:

class Employee { 
   name:string; 
 
   constructor(name:string) { 
      this.name = name;
   } 
} 
 
class Engineer extends Employee { 
   display():void { 
      console.log("Name:  "+this.name);
   } 
}
 
var obj = new Engineer("Jai"); 
obj.display();

Try it:

JS Bin on jsbin.com

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki