In this typescript tutorial we will discuss about  method overriding in typescript.

Method Overriding:

  • A method from the base (or parent) class may be replaced with a method with the same name and signature from the derived (child) class. This procedure is known as method overriding.
  • A child (derived) class method in this procedure may or may not make use of the logic specified in the parent (base) class method.
  • The super keyword can be used to call a specific method or property from the base class into a child class in order to invoke that method or property.
  • Every time we wish to modify a parent class method’s behaviour in a child class, method overriding is helpful.

Example : 

class Boy {
    name : string
    about() : void {
        console.log(this.name +” is an intelligent boy..”)
    }
}
class Employee extends Boy {
    Employeenumber : number;
    constructor(Employeenumber : number,
    name1 : string){
        super();
        this.Employeenumber = Employeenumber
        this.name = name1
    }
 displayEmployeeInformation() : void {
        console.log(“Name : “+ this.name +”,  Employeenumber : ” +
        this.Employeenumber)
    }
    about() : void{
        console.log(this.name)
    }
}
let Employe = new Employee(10, “Ajay”);
Employe.displayEmployeeInformation();
Employe.about();
 
Output :
Name : Ajay,Employeenumber :10

For any Help or Queries Contact us on info@crmonce.com or +918096556344