Skip to main content

Archive

Show more

Object-Oriented Programming in TypeScript

Object-Oriented Programming in TypeScript

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure software programs. TypeScript, being a superset of JavaScript, provides full support for OOP features such as classes, inheritance, polymorphism, encapsulation, and interfaces. This guide covers the essential OOP concepts in TypeScript.


Classes and Objects

In TypeScript, a class is a blueprint for creating objects (instances). A class can contain properties and methods. Here’s an example of defining a class and creating an object:


class Person {
  name: string;
  age: number;

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

  greet(): string {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
}

const person1 = new Person("Alice", 30);
console.log(person1.greet()); // Output: Hello, my name is Alice and I am 30 years old.

Inheritance

Inheritance allows a class to extend another class, inheriting its properties and methods. This promotes code reuse and establishes a hierarchical relationship between classes. Here’s an example of inheritance:


class Animal {
  name: string;

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

  move(distance: number): void {
    console.log(`${this.name} moved ${distance} meters.`);
  }
}

class Dog extends Animal {
  bark(): void {
    console.log("Woof! Woof!");
  }
}

const dog = new Dog("Buddy");
dog.bark(); // Output: Woof! Woof!
dog.move(10); // Output: Buddy moved 10 meters.

Encapsulation

Encapsulation is the concept of restricting access to certain components of an object. TypeScript supports encapsulation through access modifiers: public, private, and protected.


class Car {
  private brand: string;
  private model: string;

  constructor(brand: string, model: string) {
    this.brand = brand;
    this.model = model;
  }

  public getCarInfo(): string {
    return `Car: ${this.brand} ${this.model}`;
  }
}

const myCar = new Car("Toyota", "Corolla");
console.log(myCar.getCarInfo()); // Output: Car: Toyota Corolla

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon. It is often implemented using method overriding, where a subclass provides a specific implementation of a method already defined in its superclass.


class Shape {
  area(): number {
    return 0;
  }
}

class Circle extends Shape {
  radius: number;

  constructor(radius: number) {
    super();
    this.radius = radius;
  }

  area(): number {
    return Math.PI * this.radius * this.radius;
  }
}

class Rectangle extends Shape {
  width: number;
  height: number;

  constructor(width: number, height: number) {
    super();
    this.width = width;
    this.height = height;
  }

  area(): number {
    return this.width * this.height;
  }
}

const shapes: Shape[] = [new Circle(5), new Rectangle(4, 6)];
shapes.forEach(shape => {
  console.log(shape.area());
});
// Output: 78.53981633974483
// Output: 24

Interfaces

Interfaces in TypeScript define the structure of an object, enforcing the presence of certain properties and methods. They provide a way to define contracts within your code.


interface IPerson {
  name: string;
  age: number;
  greet(): string;
}

class Person implements IPerson {
  name: string;
  age: number;

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

  greet(): string {
    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
  }
}

const person2 = new Person("Bob", 25);
console.log(person2.greet()); // Output: Hello, my name is Bob and I am 25 years old.

Conclusion

Object-Oriented Programming in TypeScript provides a powerful way to structure your code using classes, inheritance, encapsulation, polymorphism, and interfaces. By leveraging these features, you can create more maintainable, scalable, and reusable code. Understanding and utilizing OOP principles in TypeScript can significantly enhance your ability to develop complex and robust applications.

Comments