Step 9 – Classes and inheritance in TypeScript

Classes in TypeScript

In TypeScript, classes provide a way to create reusable blueprints for objects. Classes define properties and methods that objects can have, and they can also define constructors that initialize the object’s properties.

Defining a class

To define a class in TypeScript, we use the class keyword followed by the class name. Here’s an example:

class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  makeSound() {
    console.log("Animal is making a sound");
  }
}

In this example, we define a class called Animal that has a name property and a makeSound method. The constructor takes a name parameter and assigns it to the name property.

Creating an instance of a class

To create an instance of a class in TypeScript, we use the new keyword followed by the class name and any arguments needed for the constructor. Here’s an example:

const myAnimal = new Animal("Rex");
console.log(myAnimal.name); // Output: "Rex"
myAnimal.makeSound(); // Output: "Animal is making a sound"

In this example, we create an instance of the Animal class called myAnimal. We pass the string “Rex” as an argument to the constructor, which sets the name property to “Rex”. We then log the name property to the console, which outputs “Rex”, and call the makeSound method, which outputs “Animal is making a sound”.

Inheritance in TypeScript

Inheritance is a way to create a new class that is a modified version of an existing class. The new class inherits all the properties and methods of the existing class, and can also have its own properties and methods.

Extending a class

To create a new class that extends an existing class in TypeScript, we use the extends keyword followed by the name of the existing class. Here’s an example:

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }
  makeSound() {
    console.log("Dog is barking");
  }
}

In this example, we define a new class called Dog that extends the Animal class. The Dog class has its own constructor that takes a name parameter and passes it to the Animal constructor using the super keyword. The Dog class also has its own makeSound method that overrides the makeSound method of the Animal class.

Creating an instance of a subclass

To create an instance of a subclass in TypeScript, we use the same syntax as creating an instance of a class, but with the subclass name instead. Here’s an example:

const myDog = new Dog("Rufus");
console.log(myDog.name); // Output: "Rufus"
myDog.makeSound(); // Output: "Dog is barking"

In this example, we create an instance of the Dog class called myDog. We pass the string “Rufus” as an argument to the Dog constructor, which sets the name property to “Rufus”. We then log the name property to the console, which outputs “Rufus”, and call the makeSound method, which outputs “Dog is barking”.

Diagram

Here’s a diagram that shows the relationship between the Animal and Dog classes:

      +------------+
      |   Animal   |
      +------------+
             ^
             |
      +------------+
      |     Dog    |
      +------------+
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.