Step 9 – Classes and inheritance in TypeScript

image 2

Play Store Application link – Java to TypeScript in 14 Steps – App on Google Play

In TypeScript, classes are used to define and create objects with shared properties and methods, much like classes in Java. They also support inheritance, allowing you to build new classes based on existing ones.

Classes in TypeScript

Classes in TypeScript let you create blueprints for objects. They include properties, methods, and constructors to initialize objects.

Defining a Class

To define a class, use the class keyword followed by the class name. Think of this like defining a class in Java using the class keyword.

Example:

class Animal {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  makeSound() {
    console.log("Animal is making a sound");
  }
}

Java Comparison:

In Java, it would look similar:

public class Animal {
  private String name;

  public Animal(String name) {
    this.name = name;
  }

  public void makeSound() {
    System.out.println("Animal is making a sound");
  }
}

Creating an Instance

To create an instance of a class, use the new keyword. This is similar to creating an object in Java with new.

Example:

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

Java Comparison:

In Java, creating an instance is also done with new:

Animal myAnimal = new Animal("Rex");
System.out.println(myAnimal.getName()); // Output: "Rex"
myAnimal.makeSound(); // Output: "Animal is making a sound"

Inheritance in TypeScript

Inheritance allows you to create a new class based on an existing class, inheriting its properties and methods, and adding or overriding them.

Extending a Class

To extend a class, use the extends keyword. This is similar to Java’s inheritance syntax.

Example:

class Dog extends Animal {
  constructor(name: string) {
    super(name);
  }

  makeSound() {
    console.log("Dog is barking");
  }
}

Java Comparison:

In Java, extending a class looks like this:

public class Dog extends Animal {
  public Dog(String name) {
    super(name);
  }

  @Override
  public void makeSound() {
    System.out.println("Dog is barking");
  }
}

Creating an Instance of a Subclass

To create an instance of a subclass, use the new keyword with the subclass name.

Example:

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

Java Comparison:

Creating an instance of a subclass in Java is done similarly:

Dog myDog = new Dog("Rufus");
System.out.println(myDog.getName()); // Output: "Rufus"
myDog.makeSound(); // Output: "Dog is barking"

Diagram

Here’s a simple diagram showing the relationship between the Animal and Dog classes:

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

In summary, classes in TypeScript are similar to Java classes, allowing you to define properties, methods, and constructors. Inheritance in TypeScript lets you create subclasses that build on existing classes, similar to how it works in Java. This allows for more modular and reusable code.

Leave a Reply

Your email address will not be published. Required fields are marked *