TypeScript Constructors and Inheritance
In TypeScript, classes are blueprints for creating objects. Constructors are special methods within a class that are automatically called when an object of that class is created. Inheritance allows a new class (child class) to inherit properties and methods from an existing class (parent class), promoting code reuse and establishing relationships between classes.
Understanding Constructors
A constructor is a method named
constructor
Constructors initialize object properties upon creation.
The constructor
method is automatically executed when you create a new instance of a class. It's the ideal place to set the initial values for your object's properties.
When you define a class in TypeScript, you can include a special method called constructor
. This method is invoked automatically when you use the new
keyword to create an instance of that class. It's commonly used to assign values to the properties of the object being created, often using parameters passed to the constructor. For example, new Car('Toyota', 'Camry')
would call the constructor
of the Car
class with 'Toyota' and 'Camry' as arguments.
To initialize the properties of an object when it is created.
Introduction to Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows you to define a new class that inherits the properties and methods of an existing class. This promotes code reusability and creates a hierarchical relationship between classes. In TypeScript, the
extends
Inheritance enables code reuse by allowing a class to inherit from another.
A child class can inherit properties and methods from a parent class using the extends
keyword. This fosters a hierarchical structure and reduces redundant code.
Inheritance allows a class (the subclass or child class) to inherit the members (properties and methods) of another class (the superclass or parent class). This is achieved using the extends
keyword. The child class can then add its own unique properties and methods, or override the inherited ones to provide specific implementations. This mechanism is crucial for building complex systems and managing code efficiently.
Using `super` in Constructors
When a child class has a constructor, it must call the parent class's constructor using the
super()
Consider a Vehicle
class with a make
property and a startEngine
method. A Car
class can inherit from Vehicle
. The Car
constructor needs to call super(make)
to initialize the make
property inherited from Vehicle
, and then can add its own specific properties like numberOfDoors
.
Text-based content
Library pages focus on text content
super()
Method Overriding
Inheritance also allows a child class to provide a specific implementation for a method that is already defined in its parent class. This is known as method overriding. The child class's version of the method will be executed when called on an instance of the child class.
Method overriding allows for polymorphism, where different classes can respond to the same method call in their own unique ways.
Putting It All Together: Example
Let's illustrate with a simple example. We'll create a
Person
Employee
Person
Loading diagram...
In this structure, the
Employee
Person
Employee
super()
Person
Employee
Person
Learning Resources
The official TypeScript documentation provides a comprehensive overview of classes, including constructors, inheritance, and access modifiers.
A focused section within the official docs detailing how inheritance works in TypeScript, including the use of `extends` and `super`.
While focused on JavaScript, MDN's explanation of class inheritance is highly relevant and provides a solid foundation for understanding TypeScript's implementation.
This tutorial offers a clear explanation of constructors in TypeScript, covering their syntax, purpose, and common usage patterns.
A blog post that specifically breaks down the `super` keyword in TypeScript, explaining its role in constructors and method calls.
A video tutorial that visually explains the concepts of classes and inheritance in TypeScript, often with practical coding examples.
This article delves into method overriding in TypeScript, explaining how child classes can redefine parent class methods.
GeeksforGeeks provides a detailed explanation of inheritance in TypeScript, including examples of single and multilevel inheritance.
A practical guide from CodeMentor covering essential OOP concepts in TypeScript, with a focus on constructors and inheritance.
W3Schools offers a beginner-friendly introduction to TypeScript classes, covering constructors, properties, and methods.