The main advantage of inheritance is to the developer. This is because, it enables code reuse. To inherit, we use the extends keyword. The following example the class Visakhapatnam extends the City class. Refer to it in the following link:
https://sashankexpresstech.blogspot.com/2018/11/typescript-classes-public-methods-and.html
All the members of the City class got inherited by Visakhapatnam class. The constructor is also inherited by the child class. To modify the private and protected properties of the object. We need to use the set methods defined to modify them.
Reference:
https://sashankexpresstech.blogspot.com/2018/11/typescript-classes-public-methods-and.html
class Visakhapatnam extends City{
name = "Visakhapatnam";
protected elevation = 45;
}
const cityVisakha = new Visakhapatnam("Visakhapatnam");
console.log(cityVisakha);
console.log(cityVisakha.name);
cityVisakha.setType("Town");
console.log(cityVisakha.printType());
console.log(cityVisakha.getElevation());
console.log(cityVisakha);
All the members of the City class got inherited by Visakhapatnam class. The constructor is also inherited by the child class. To modify the private and protected properties of the object. We need to use the set methods defined to modify them.
Reference:
- https://www.typescriptlang.org/docs/handbook/classes.html
No comments:
Post a Comment