Access Modifiers
Access Modifiers
Section titled “Access Modifiers”Introduction
Section titled “Introduction”Access modifiers (also called access specifiers) determine the visibility and accessibility of class members (properties and methods). They are fundamental to encapsulation.
Common Access Modifiers
Section titled “Common Access Modifiers”| Modifier | Same Class | Subclass | External |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |
Code Examples
Section titled “Code Examples”class BankAccount { #balance = 0; // Private field (ES2020+)
deposit(amount) { this.#balance += amount; } getBalance() { return this.#balance; }}class Employee { public name: string; // Accessible everywhere private salary: number; // Only within this class protected department: string; // Within class and subclasses readonly id: number; // Can only be set in constructor}class User: def __init__(self, username, email, password): self.username = username # Public self._email = email # Protected (convention) self.__password = password # Private (name mangling)Best Practices
Section titled “Best Practices”- Default to private — Make fields private; expose only what’s needed
- Use public for API — Public members are your contract with consumers
- Use protected sparingly — Protected creates coupling to subclasses
- Provide getters/setters instead of making fields public
Previous Topic: Advanced Concepts → Next Topic: Static Members →