0
0
Typescriptprogramming~5 mins

Access modifiers public private protected in Typescript

Choose your learning style9 modes available
Introduction

Access modifiers control who can see or change parts of your code. They help keep your data safe and organized.

When you want everyone to use or change a value or function, use <strong>public</strong>.
When you want to hide data or functions inside a class so only that class can use them, use <strong>private</strong>.
When you want to share data or functions with a class and its children (subclasses), but not with others, use <strong>protected</strong>.
Syntax
Typescript
class ClassName {
  public propertyName: type;
  private propertyName: type;
  protected propertyName: type;

  public methodName() {}
  private methodName() {}
  protected methodName() {}
}

public is the default if you don't write any modifier.

Access modifiers apply only inside classes.

Examples
This class shows all three modifiers on properties and methods.
Typescript
class Person {
  public name: string;
  private age: number;
  protected city: string;

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

  public greet() {
    console.log(`Hello, my name is ${this.name}`);
  }

  private getAge() {
    return this.age;
  }

  protected getCity() {
    return this.city;
  }
}
Outside the class, you can only access public members.
Typescript
const p = new Person('Alice', 30, 'Paris');
console.log(p.name); // works because name is public
// console.log(p.age); // error: age is private
// console.log(p.city); // error: city is protected
Child classes can access protected members but not private.
Typescript
class Employee extends Person {
  showCity() {
    console.log(this.getCity()); // works because getCity is protected
  }
}
Sample Program

This program shows how public, private, and protected work in classes and subclasses.

Typescript
class Animal {
  public name: string;
  private sound: string;
  protected habitat: string;

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

  public makeSound() {
    console.log(`${this.name} says ${this.sound}`);
  }

  private secretSound() {
    console.log('This is a secret sound');
  }

  protected getHabitat() {
    return this.habitat;
  }
}

class Bird extends Animal {
  showHabitat() {
    console.log(`${this.name} lives in ${this.getHabitat()}`);
  }
}

const parrot = new Bird('Parrot', 'Squawk', 'tropical forests');
parrot.makeSound();
parrot.showHabitat();
// parrot.secretSound(); // Error: private method
// console.log(parrot.sound); // Error: private property
// console.log(parrot.habitat); // Error: protected property
OutputSuccess
Important Notes

Trying to access private or protected members outside allowed places causes errors.

protected is like a secret shared only with child classes.

Use these modifiers to keep your code safe and clear.

Summary

public means anyone can use it.

private means only the class itself can use it.

protected means the class and its children can use it.