Is TypeScript OOP?

Alex Rivers

TypeScript? OOP? Many people in the computer science world have had a discourse on whether TypeScript is OOP or not.

OOP stands for Object Oriented Programming. Some people even call it OOC, which is short for Object Oriented Concepts. To better understand this, OOP is a programming paradigm in which a program is characterized by the identification of classes of objects which are closely linked with the methods or functions. In order to come under OOP, a programming language must follow some or all the following characteristics:

  • Classes
  • Objects
  • Abstraction
  • Inheritance
  • Polymorphism (Compile-time Polymorphism, Run-time Polymorphism)
  • Encapsulation

Now, TypeScript is an object-oriented, open-source programming language and an extension of Javascript. It is a well-known fact that TS (TypeScript) is a superset of JS (JavaScript).

Let’s see if TypeScript follows any of the above-mentioned OOP characters.

Classes and Objects

Class is a user-defined data type that holds its own data members and member functions which can be accessed and used by creating an instance of that class. An object is an instance of a class.

It is also called a blueprint for an object.

Here is a code snippet that will display the creation of a class, followed by a constructor:

class Room {
  public furniture: string;
  public color: string;
  public isElectricity: boolean;

  constructor(furniture: string, color: string, isElectricity: boolean) {
    this.furniture= furniture;
    this.color = color;
    this.isElectricity = isElectricity;
  }

Abstraction

In this, the implementation part of the code is hidden. Only the part that is necessary is shown to the user. Whatever lies underneath it is not shown. There are multiple ways to reach abstraction- by classes or methods, interfaces and types.

Here is an example that shows how abstraction works TS:

abstract class Flying {
  public pilot: string;
  public flightSpeed: number;

  constructor(pilot: string, flightSpeed: number) {
    this.pilot = pilot;
    this.flightSpeed = flightSpeed;
  }

  public abstract takeOff(): number;
}

class Landing extends Flying {
  constructor(pilot: string, flightSpeed: number) {
    super(pilot, flightSpeed);
  }

  public takeOff(): number {
    return this.flightSpeed;
  }
}

The abstract class called ‘takeOff’ is just a definition class. Our class ‘Landing’ is depending on this abstract class.

Inheritance

When a class is derived from another class, it is called inheritance. The child class will inherit all the public and protected properties, as well as, methods from the parent class. It can also have its own properties and methods.

An inherited class is defined by using the extends keyword.

Here is a part of the code demonstrating Inheritance:

class Landing extends Flying {
  public pilot: string;

  constructor(name: string, airline: number, pilot:string) {
    super(name, airline);

    this.pilot = pilot;
  }

  public Fly(): void {
    console.log(`The name of the pilot is ${this.pilot}.`);
  }

Polymorphism

It is the ability to create a class in more than one form. In short, it means one can access objects of different types through the same interface.

Here is a code snippet showing Polymorphism in TS:

class Flying {
  public name: string;
  public speed: number;

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

  public takeOff(): void {
    console.log('Taking off in...');
  }

  public Landing(): void {
    console.log(`Landing in....`);
  }
}

class Pilot extends Flying {
  public airline: string;

  constructor(name: string, speed: number, airline: string) {
    super(name, speed);

    this.airline = airline;
  }

  public takeOff(): void {
    console.log('The aircraft will take off in....');
  }

  public Landing(): void {
    console.log(`Landing aircraft is ${this.airline}.`);
  }
}

Encapsulation

Encapsulation means hiding the data. In this, we restrict access to certain properties/methods.

Here is a code snippet describing encapsulation:


class Flying {
  private airline: string;

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

  public get airline(): string {
    return this.airline;
  }

  public set airline(value: string) {
    this.airline = value;
  }
}

Conclusion

From our discussion, we have understood that TS exhibits object-oriented programming properties. Thus, it is a multiparadigm programming language and follows all the OOP principles. It has shown the creation of classes & objects, abstraction, inheritance, polymorphism and encapsulation.

Happy Coding!

Also check: Is TypeScript a framework?

Leave a Comment