Inheritance and Polymorphism in TypeScript

  • Just like other languages you can extend a classes which will have is a relationship with each other.
  • All the methods and data members are inherited from the parent class.
class Person{
    name : String;
    greet(){
        console.log("Hello Person");
    }
}
class Employee extends Person{
    empId : String;
    greet(){
        console.log("Hello Employee")
    }
    personGreet(){
        super.greet();
    }
}
var emp = new Employee();
emp.personGreet();
emp.greet();
var per : Person = new Employee();
emp.personGreet(); //greet on employee method
emp.name = "Tyson Gill" //inheriting the data memeber of parent class
emp.empId = "879869689698";

Leave a Comment