How to use inheritance using TypeScript?
Class based object oriented programming can be done through TypeScript. It can be used in any JavaScript host. Application-scale JavaScript applications can be developed by using TypeScript. I hope it will be useful for you all.
Description:
Vehicle is main class and bus is extended from Vehicle class.
ServiceAfterDays method is used to display number of days required to do servicing for particular type of vehicle.
Inheritance maintains parent-child relationship between two or more classes. Child class properties have properties of its own as well as properties of base class.
class Vehicle {
constructor(public type) { }
ServiceAfterDays(days)
{
alert(this.type + " ServiceAfterDays " + days);
}
}
class Bus extends Vehicle {
constructor(type)
{
super(type);
}
ServiceAfterDays()
{
alert("Happy journey");
super.ServiceAfterDays(500);
}
}
class Car extends Vehicle
{
constructor(type)
{ super(type);
}
ServiceAfterDays()
{
alert("Safe Drive");
super.ServiceAfterDays(45);
}
}
var Kamal = new Bus("Municiple Bus")
var Vimal: Vehicle = new Car("Smooth Car")
Kamal.ServiceAfterDays()
Vimal.ServiceAfterDays(34)