Topics in this subject
TypeScript 3 min read Updated 11 Aug 2026

Classes and Modifiers

Public, private, protected, readonly, and abstract classes in TypeScript.

🧑‍🏫 Sabse pehle — simple mein samjho#

JavaScript (ES6) mein classes hoti hain, par wo aapas mein bahut khuli-duli hoti hain — koi bhi data access kar sakta hai. TypeScript classes mein Access Modifiers (security guards) lagata hai. Isse tum control kar sakte ho ki class ke andar ka data bahar kon dekh sakta hai aur kon nahi. React component classes (ab purani ho chuki hain, Hooks better hain) mein inka zyada use nahi hota tha, par Node.js (NestJS) ya general OOP pattern mein ye jaan hain.

Access Modifiers (The Security Guards)#

TypeScript has 3 main access modifiers:

  1. public (Default) — Open to everyone. Can be accessed from both inside and outside the class.
  2. private — Can only be accessed from within the class it is defined. It cannot be accessed from outside via an instance (object).
  3. protected — Can be accessed from within the class and its inherited sub-classes (children). It cannot be accessed from outside.
class BankAccount {
  public ownerName: string; // Accessible anywhere
  private balance: number; // Hidden (Accessible only within BankAccount)
  protected accountType: string; // Hidden, but accessible to inherited classes

  constructor(ownerName: string, balance: number, accountType: string) {
    this.ownerName = ownerName;
    this.balance = balance;
    this.accountType = accountType;
  }

  public getBalance() {
    return this.balance; // 'private' is allowed to be accessed inside the class
  }
}

const myAcc = new BankAccount("Tarun", 5000, "Savings");
console.log(myAcc.ownerName); // ✅ "Tarun"
// console.log(myAcc.balance); // ❌ ERROR: Property 'balance' is private
// console.log(myAcc.accountType); // ❌ ERROR: Property 'accountType' is protected

Parameter Properties (Shortcut 🚀)#

In the code above, writing the variables in the constructor and then assigning them at the class level (this.x = x) is repetitive. TypeScript provides a shortcut. If you add an access modifier (like public or private) directly inside the constructor parameters, TS will automatically create and initialize the property for you.

class BetterBankAccount {
  // Shortcut! These automatically become class properties
  constructor(
    public ownerName: string, 
    private balance: number, 
    protected accountType: string
  ) {}
}

Readonly properties#

If you want to ensure that a property cannot be changed after the object is created, use readonly.

class User {
  constructor(public readonly id: number, public name: string) {}
}

const u = new User(1, "Tarun");
// u.id = 2; // ❌ ERROR: Cannot assign to 'id' because it is a read-only property.

Implements (Enforcing Interfaces on Classes)#

You can use the implements keyword to force a class to follow a specific interface. It ensures the class contains all the required properties and methods defined in that interface.

interface Printable {
  print(): void;
}

class Invoice implements Printable {
  print() {
    console.log("Printing invoice...");
  }
}

Abstract Classes#

Abstract classes cannot be directly instantiated (you cannot use new on them). They serve solely as base classes to guide (extend) other classes.

abstract class Shape {
  // Every child (subclass) MUST implement this method
  abstract getArea(): number;
  
  // A normal method that is inherited by all subclasses
  printName() { console.log("I am a shape"); }
}

// const s = new Shape(); // ❌ ERROR: Cannot create an instance of an abstract class.

class Circle extends Shape {
  constructor(public radius: number) { super(); }
  
  getArea() {
    return Math.PI * this.radius * this.radius;
  }
}