Webx Experiment 3
Webx Experiment 3
Aim:
Write a program in TypeScript to implement inheritance.
Theory
2. Child Class (Sub Class): A child class extends the parent class and inherits its properties
and methods. It can also introduce new attributes and behaviors or modify existing ones by
overriding methods from the parent class.
Advantages of Inheritance
- Code Reusability: Instead of writing the same code multiple times, common functionality
can be inherited from a parent class.
- Maintainability: Changes in the parent class automatically reflect in child classes, reducing
the need for code duplication.
2. Multilevel Inheritance: A child class inherits from another child class, forming a chain of
inheritance.
3. Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
Method Overriding in Inheritance
When a child class redefines a method from the parent class to provide its own specific
implementation, it is called method overriding. This allows customization of inherited
behavior while maintaining a shared structure.
- Animal Classification: A 'Mammal' class can define common traits, while subclasses like
'Dog' and 'Cat' extend it with specific behaviors.
- Banking Application: A 'BankAccount' class can define general account operations, while
'SavingsAccount' and 'CurrentAccount' extend it with unique features.
Conclusion
Inheritance in TypeScript is a powerful feature that enhances code reusability, modularity,
and maintainability. By defining a parent class and extending it to child classes, developers
can create structured and scalable applications. Understanding inheritance helps in building
robust OOP-based TypeScript applications.