Lesson 49 of 60 – Access Modifiers
82%

Access Modifiers in TypeScript

Access modifiers in TypeScript control where class properties and methods can be accessed. They are mainly used to control the visibility and accessibility of members inside a class.

Note: TypeScript provides three main access modifiers: public, private, and protected.

1. What are Access Modifiers?

Access modifiers define how class properties and methods can be accessed.

class Student {
    public name: string;
    private age: number;
    protected course: string;
}

Each modifier provides a different level of access.

2. Types of Access Modifiers

TypeScript mainly provides three access modifiers:

  • public
  • private
  • protected

They control access to properties and methods.

3. public Modifier

The public modifier allows a member to be accessed from anywhere.

class Student {
    public name: string;

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

4. Accessing a public Property

const student = new Student("Rahul");

console.log(student.name);

The name property can be accessed outside the class because it is public.

5. public is the Default Modifier

If no access modifier is specified, a class member is public by default.

class Student {
    name: string;
}

This is equivalent to:

class Student {
    public name: string;
}

6. private Modifier

The private modifier allows a member to be accessed only inside its class.

class Account {
    private balance: number;

    constructor(balance: number) {
        this.balance = balance;
    }
}

7. Accessing a private Property

class Account {
    private balance: number;

    constructor(balance: number) {
        this.balance = balance;
    }
}

const account = new Account(5000);

// Error
// console.log(account.balance);

The private property cannot be accessed directly outside the class.

8. Accessing private Data Through a Method

A public method can be used to access or work with private data inside the class.

class Account {
    private balance: number;

    constructor(balance: number) {
        this.balance = balance;
    }

    getBalance(): number {
        return this.balance;
    }
}

const account = new Account(5000);

console.log(account.getBalance());

9. Why Use private?

The private modifier helps protect internal data from direct access.

  • Protects class data.
  • Prevents unwanted direct changes.
  • Improves encapsulation.
  • Controls how data is accessed.

10. protected Modifier

The protected modifier allows access inside the class and inside classes that inherit from it.

class Person {
    protected name: string;

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

11. protected in a Child Class

class Person {
    protected name: string;

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

class Student extends Person {
    showName(): void {
        console.log(this.name);
    }
}

The child class can access the protected property.

12. protected Cannot Be Accessed Directly

const student = new Student("Amit");

// Error
// console.log(student.name);

A protected property cannot be accessed directly from outside the class hierarchy.

13. public vs private

class Student {
    public name: string;
    private age: number;

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

name can be accessed outside the class, while age cannot.

14. public vs protected

class Person {
    public name: string;
    protected age: number;

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

The public property is accessible everywhere, while the protected property is available inside the class and its derived classes.

15. private vs protected

Both modifiers restrict outside access, but they differ when inheritance is involved.

  • private → only the declaring class.
  • protected → declaring class and derived classes.

16. Access Modifiers on Methods

Access modifiers can also be used with methods.

class Student {
    public show(): void {
        console.log("Student");
    }

    private calculate(): void {
        console.log("Calculating");
    }
}

17. Private Method

class Student {
    private calculateMarks(): number {
        return 450;
    }

    showResult(): void {
        console.log(this.calculateMarks());
    }
}

The private method can be called inside the class but not directly from outside.

18. protected Method

class Person {
    protected showDetails(): void {
        console.log("Person details");
    }
}

class Student extends Person {
    display(): void {
        this.showDetails();
    }
}

19. public Method

class Student {
    public showName(): void {
        console.log("Rahul");
    }
}

const student = new Student();

student.showName();

A public method can be called from outside the class.

20. Access Modifiers in Constructors

TypeScript allows access modifiers to be used directly with constructor parameters.

class Student {
    constructor(
        public name: string,
        private age: number
    ) {
    }
}

21. public Constructor Parameter

class Student {
    constructor(public name: string) {
    }
}

const student = new Student("Priya");

console.log(student.name);

The public parameter automatically becomes a public class property.

22. private Constructor Parameter

class Account {
    constructor(private balance: number) {
    }

    getBalance(): number {
        return this.balance;
    }
}

The balance can be used internally but is not directly accessible outside the class.

23. protected Constructor Parameter

class Person {
    constructor(protected name: string) {
    }
}

class Student extends Person {
    showName(): void {
        console.log(this.name);
    }
}

The protected property can be accessed by the derived class.

24. Access Modifiers and Encapsulation

Access modifiers are an important part of encapsulation. Encapsulation means keeping data and the operations that work with that data together while controlling access.

class BankAccount {
    private balance: number;

    constructor(balance: number) {
        this.balance = balance;
    }

    getBalance(): number {
        return this.balance;
    }
}

25. Controlling Data Changes

A private property can be changed through controlled methods instead of allowing direct modification.

class BankAccount {
    private balance: number;

    constructor(balance: number) {
        this.balance = balance;
    }

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

    getBalance(): number {
        return this.balance;
    }
}

26. Practical Employee Example

class Employee {
    public name: string;
    private salary: number;

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

    getSalary(): number {
        return this.salary;
    }
}

const employee = new Employee("Amit", 30000);

console.log(employee.name);
console.log(employee.getSalary());

27. Choosing the Right Modifier

  • Use public when outside access is required.
  • Use private when only the class should access the member.
  • Use protected when the class and derived classes need access.

Choosing the appropriate modifier helps keep a class organized and controlled.

28. Common Access Modifier Mistakes

  • Trying to access a private property outside its class.
  • Trying to access a protected property directly from an object.
  • Using public when internal data should be protected.
  • Forgetting that public is the default modifier.
  • Confusing private and protected during inheritance.

29. Access Modifier Comparison

Modifier Inside Class Derived Class Outside
public Yes Yes Yes
protected Yes Yes No
private Yes No No

30. Complete Access Modifier Example

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 showAge(): void {
        console.log("Age:", this.age);
    }
}

class Student extends Person {
    showCity(): void {
        console.log("City:", this.city);
    }
}

const student = new Student(
    "Rahul",
    20,
    "Aurangabad"
);

console.log(student.name);
student.showAge();
student.showCity();

In this example, name is public, age is private, and city is protected. The derived class can access city, but not age.

📌 Key Points

  • Access modifiers control access to class members.
  • TypeScript provides public, private, and protected.
  • public members can be accessed from anywhere.
  • public is the default access modifier.
  • private members can only be accessed inside their class.
  • protected members can be accessed inside the class and derived classes.
  • Access modifiers can be used with properties and methods.
  • Access modifiers can also be used with constructor parameters.
  • They help implement encapsulation.
  • Choosing the correct modifier helps control and protect class data.

🧠 Quick Quiz

Question: Which access modifier allows a member to be accessed inside the class and inside derived classes?