typescript how to call a public function from subclass code example
Example 1: ts override method
class Person{
name:string
eat():void{
console.log(this.name+" eats when hungry.")
}
}
class Student extends Person{
rollnumber:number;
constructor(rollnumber:number, name1:string){
super();
this.rollnumber = rollnumber
this.name = name1
}
displayInformation():void{
console.log("Name : "+this.name+", Roll Number : "+this.rollnumber)
}
eat():void{
console.log(this.name+" eats during break.")
}
}
var student1 = new Student(2, "Rohit")
student1.displayInformation()
student1.eat()
Example 2: class inheritance in typescript
class Info {
protected name: string ;
constructor(n:string){
this.name = n ;
};
describe(){
console.log(`Your name is ${this.name}`);
}
}
class Detail extends Info{
constructor(name:string, public age:number){
super(name);
}
findAge(){
console.log(`${this.name} age is ${this.age}`)
}
}
const b = new Detail('jank', 23);
b.describe();
b.findAge();