TypeScript Tutorial
Estimated reading: 4 minutes 35 views

5️⃣ 🧩 TypeScript – Interfaces & Classes (OOP) Explained with Examples (2025)


🧲 Introduction – Why Learn TypeScript OOP?

Object-Oriented Programming (OOP) in TypeScript combines the power of structured typing with classical OOP features like classes, interfaces, inheritance, and access control. Whether you’re building web apps or libraries, mastering TypeScript’s interface and class system gives you tools to model complex systems with clarity, reusability, and type safety.

🎯 In this guide, you’ll learn:

  • How to define and extend interfaces in TypeScript
  • Object-oriented principles using TypeScript classes
  • Use of modifiers like public, private, readonly, and static
  • Inheritance, abstract classes, and duck typing in real projects

📘 Topics Covered

🧩 Topic📌 Description
TS InterfacesDefine contracts for objects, functions, and classes
Extending InterfacesCombine and reuse interface structures
TS ClassesCreate OOP-style classes with properties and methods
TS ObjectsDeclare typed objects using interfaces or type annotations
Access ModifiersControl property access with public, private, protected
Readonly PropertiesProtect data with readonly decorators
TS InheritanceDerive child classes from base classes
Static MembersUse static methods/properties that belong to class, not instance
Abstract ClassesDefine partial base classes with abstract methods
Accessors (get/set)Define getter and setter functions for class properties
Duck TypingStructural typing without explicitly implementing interfaces

🧩 TypeScript Interfaces – Define Contracts

interface User {
  name: string;
  age: number;
}

const u: User = {
  name: "Alice",
  age: 30,
};

✅ Interfaces enforce object structure without implementing logic.


🔗 Extending Interfaces – Reuse and Expand

interface Employee extends User {
  employeeId: string;
}

🔄 Interfaces can extend other interfaces or multiple ones.


🏗️ TypeScript Classes – OOP Made Simple

class Car {
  brand: string;
  constructor(brand: string) {
    this.brand = brand;
  }

  drive(): void {
    console.log(`${this.brand} is driving`);
  }
}

💡 Use new keyword to create class instances.


🧱 Object Typing with Interfaces

interface Book {
  title: string;
  author: string;
}

const b1: Book = {
  title: "TS Mastery",
  author: "Vaibhav",
};

📚 Interface types can be used for any object-based structure.


🔒 Access Modifiers – Encapsulation in Classes

class BankAccount {
  private balance: number = 0;

  deposit(amount: number) {
    this.balance += amount;
  }

  getBalance(): number {
    return this.balance;
  }
}
  • public: accessible anywhere
  • private: only within the class
  • protected: within the class & subclasses

🔐 Readonly Properties – Immutable by Design

class Person {
  readonly id: number;
  constructor(id: number) {
    this.id = id;
  }
}

✅ Readonly values can only be assigned once.


🧬 Inheritance – Class Reuse and Extension

class Animal {
  move() {
    console.log("Animal is moving");
  }
}

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

Use extends for inheritance and super() for parent constructor.


🛠️ Static Methods & Properties

class Utility {
  static version: string = "1.0";
  static greet() {
    return "Hello!";
  }
}

console.log(Utility.version);
console.log(Utility.greet());

🔧 Accessed on the class itself, not instances.


🧱 Abstract Classes – Define Blueprints

abstract class Shape {
  abstract area(): number;
}

class Circle extends Shape {
  constructor(public radius: number) {
    super();
  }

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

Abstract classes can’t be instantiated directly.


🧪 Accessors – Getter & Setter Methods

class Product {
  private _price: number = 0;

  get price() {
    return this._price;
  }

  set price(value: number) {
    if (value >= 0) this._price = value;
  }
}

Use get and set for controlled access.


🦆 Duck Typing – Structural Typing in Action

interface Flyer {
  fly(): void;
}

let bird = {
  fly: () => console.log("Flying high!"),
};

function startFlying(entity: Flyer) {
  entity.fly();
}

startFlying(bird); // ✅ allowed, matches structure

✅ TypeScript uses structural typing, not nominal typing.


📌 Summary – Recap & Next Steps

TypeScript blends the best of interfaces and class-based OOP with static typing. It’s perfect for teams that need both structure and flexibility in large projects.

🔍 Key Takeaways:

  • Interfaces define structure, not implementation
  • Classes support inheritance, access control, and abstraction
  • readonly, private, protected, and static enable better design
  • Duck typing allows flexible function inputs via structural checks

⚙️ Real-World Relevance:
TypeScript’s OOP features scale beautifully in enterprise apps, allowing predictable, testable, and modular development.


❓ FAQ – Interfaces & Classes in TypeScript

❓ What is the main difference between an interface and a class?

✅ Interfaces define shape/structure; classes define implementation with behavior.


❓ Can interfaces extend classes in TypeScript?

✅ No, but interfaces can extend other interfaces. Classes implement interfaces.


❓ When to use abstract classes vs. interfaces?

✅ Use abstract when you need partial implementation and inheritance; use interfaces for pure contracts.


❓ What is duck typing in TypeScript?

✅ If an object matches the required structure, it’s considered valid—even if it doesn’t explicitly implement an interface.


Share Now :

Leave a Reply

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

Share

5️⃣ 🧩 TypeScript — Interfaces & Classes (OOP)

Or Copy Link

CONTENTS
Scroll to Top