🔒 Access Specifiers
Last Updated: 24th October 2025
Access Specifiers (or Access Modifiers) are used to control the visibility of class properties and methods.
They define who can access what — inside or outside the class.
Hinglish Tip 🗣: Access Specifier ek “security guard” jaisa hai — jo decide karta hai kaunsa data class ke bahar dikhana hai aur kaunsa hidden rakhna hai.
🧩 1. Public Members
- Default in JavaScript.
- Can be accessed anywhere — inside or outside the class.
- No special symbol needed.
Example:
class Student {
constructor(name, age) {
this.name = name; // public
this.age = age; // public
}
show() {
console.log(`Name: ${this.name}, Age: ${this.age}`);
}
}
const s1 = new Student("Riya", 20);
console.log(s1.name); // ✅ Accessible
s1.show(); // ✅ Accessible
Hinglish Tip 🗣: Public members sabko dikhte hain — class ke bahar se bhi access ho sakte hain.
🕵️♂️ 2. Private Members (#)
- Declared using # (hash symbol).
- Can only be accessed inside the class.
- Helps hide sensitive data like passwords, balance, etc.
- Introduced in ES2022.
class BankAccount {
#balance = 1000; // private property
#showBalance() {
// private method
console.log(`Current Balance: ${this.#balance}`);
}
accessBalance() {
this.#showBalance(); // ✅ accessible inside
}
}
const acc = new BankAccount();
acc.accessBalance(); // ✅ Works
// console.log(acc.#balance); ❌ Error: Private field '#balance' must be declared
Hinglish Tip 🗣: # lagao aur data ko “lock” kar do — sirf class ke andar ka code hi use dekh sakta hai.
3. Protected Members (by Convention)
- JavaScript doesn’t officially support protected members.
- But developers follow a naming convention using _ (underscore).
\_propertyName→ means “treat this as protected”.- Accessible by subclasses but should not be used directly outside.
class Person {
constructor(name) {
this._name = name; // protected (by convention)
}
}
class Employee extends Person {
showName() {
console.log(`Employee: ${this._name}`); // ✅ accessible in subclass
}
}
const e1 = new Employee("Amit");
e1.showName(); // ✅ Works
console.log(e1._name); // ⚠️ Works, but not recommended
Hinglish Tip 🗣: _underscore ka matlab hota hai — “main protected hu, please mujhe directly mat chuo!”
Getter & Setter Methods
- Used to get and set object properties safely.
- Defined using the keywords get and set.
- Commonly used for data validation or computed values.
class Employee {
constructor(name, salary) {
this.name = name;
this._salary = salary;
}
// getter
get salary() {
return this._salary;
}
// setter
set salary(value) {
if (value < 0) {
console.log("Salary cannot be negative!");
} else {
this._salary = value;
}
}
}
const emp = new Employee("Amit", 50000);
console.log(emp.salary); // getter → 50000
emp.salary = 60000; // setter → updated
console.log(emp.salary); // 60000
Hinglish Tip 🗣: Getter “value lene” ke liye aur Setter “value set/update karne” ke liye hota hai
💡 Quick Practice
- Create a Car class with:
- a constructor (brand, model)
- an instance method info()
- a static method compare(car1, car2) that compares model names.
- Make a class Circle with:
- radius property
- get area() → returns area
- set radius() → prevents negative radius.
- Create a class with one private method that logs a secret message.
- Create a class User with:
- #password as private
- username as public
- a method checkPassword(input) that validates password
- Exercise