LibraryConstructors and inheritance

Constructors and inheritance

Learn about Constructors and inheritance as part of TypeScript Full-Stack Development

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

code
constructor
within a class. It's used to initialize the object's properties when it's instantiated. You can pass arguments to the constructor to set initial values for the object's state.

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.

What is the primary purpose of a constructor in a TypeScript class?

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

code
extends
keyword is used to establish inheritance.

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

code
super()
keyword. This ensures that the parent class's initialization logic is executed before the child class's specific initialization.

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

What keyword must be used in a child class's constructor to call the parent class's constructor?

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

code
Person
class and then an
code
Employee
class that inherits from
code
Person
.

Loading diagram...

In this structure, the

code
Employee
class inherits from
code
Person
. The
code
Employee
constructor must call
code
super()
to initialize the
code
Person
part of the object. The
code
Employee
class can also override methods from
code
Person
to provide specialized behavior.

Learning Resources

TypeScript Official Documentation: Classes(documentation)

The official TypeScript documentation provides a comprehensive overview of classes, including constructors, inheritance, and access modifiers.

TypeScript Inheritance Explained(documentation)

A focused section within the official docs detailing how inheritance works in TypeScript, including the use of `extends` and `super`.

MDN Web Docs: Class Inheritance(documentation)

While focused on JavaScript, MDN's explanation of class inheritance is highly relevant and provides a solid foundation for understanding TypeScript's implementation.

TypeScript Constructors Tutorial(tutorial)

This tutorial offers a clear explanation of constructors in TypeScript, covering their syntax, purpose, and common usage patterns.

Understanding `super` in TypeScript(blog)

A blog post that specifically breaks down the `super` keyword in TypeScript, explaining its role in constructors and method calls.

Learn TypeScript: Classes and Inheritance(video)

A video tutorial that visually explains the concepts of classes and inheritance in TypeScript, often with practical coding examples.

TypeScript Method Overriding Explained(blog)

This article delves into method overriding in TypeScript, explaining how child classes can redefine parent class methods.

GeeksforGeeks: Inheritance in TypeScript(blog)

GeeksforGeeks provides a detailed explanation of inheritance in TypeScript, including examples of single and multilevel inheritance.

TypeScript OOP Concepts: Constructors and Inheritance(blog)

A practical guide from CodeMentor covering essential OOP concepts in TypeScript, with a focus on constructors and inheritance.

W3Schools: TypeScript Classes(tutorial)

W3Schools offers a beginner-friendly introduction to TypeScript classes, covering constructors, properties, and methods.