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.
public, private, and protected.
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.
TypeScript mainly provides three access modifiers:
publicprivateprotectedThey control access to properties and methods.
The public modifier allows a member to be
accessed from anywhere.
class Student {
public name: string;
constructor(name: string) {
this.name = name;
}
}
const student = new Student("Rahul");
console.log(student.name);
The name property can be accessed outside
the class because it is public.
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;
}
The private modifier allows a member to be
accessed only inside its class.
class Account {
private balance: number;
constructor(balance: number) {
this.balance = balance;
}
}
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.
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());
The private modifier helps protect internal
data from direct access.
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;
}
}
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.
const student = new Student("Amit");
// Error
// console.log(student.name);
A protected property cannot be accessed directly from outside the class hierarchy.
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.
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.
Both modifiers restrict outside access, but they differ when inheritance is involved.
private → only the declaring class.protected → declaring class and derived classes.Access modifiers can also be used with methods.
class Student {
public show(): void {
console.log("Student");
}
private calculate(): void {
console.log("Calculating");
}
}
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.
class Person {
protected showDetails(): void {
console.log("Person details");
}
}
class Student extends Person {
display(): void {
this.showDetails();
}
}
class Student {
public showName(): void {
console.log("Rahul");
}
}
const student = new Student();
student.showName();
A public method can be called from outside the class.
TypeScript allows access modifiers to be used directly with constructor parameters.
class Student {
constructor(
public name: string,
private age: number
) {
}
}
class Student {
constructor(public name: string) {
}
}
const student = new Student("Priya");
console.log(student.name);
The public parameter automatically becomes
a public class property.
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.
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.
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;
}
}
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;
}
}
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());
public when outside access is required.private when only the class should access the member.protected when the class and derived classes need access.Choosing the appropriate modifier helps keep a class organized and controlled.
| Modifier | Inside Class | Derived Class | Outside |
|---|---|---|---|
public |
Yes | Yes | Yes |
protected |
Yes | Yes | No |
private |
Yes | No | No |
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.
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.Question: Which access modifier allows a member to be accessed inside the class and inside derived classes?