← Back to TypeScript Course | Chapter 6: Classes | Lesson 3 of 9

Access Modifiers (public, private, protected) की जानकारी

Access modifiers control करते हैं कि class members कहां access किए जा सकते हैं। TypeScript visibility के अलग-अलग levels express करने के लिए public, private, और protected modifiers प्रदान करता है।
Syntax
typescript
class ClassName {
  public publicProp: type;
  private privateProp: type;
  protected protectedProp: type;
}

Public Members

Public members कहीं से भी access किए जा सकते हैं जहां object खुद accessible है, और public TypeScript classes में default visibility है जब कोई modifier बिल्कुल नहीं लिखा जाता।

उदाहरण: Public Members

typescript
class Person {
  public name: string = "Guest"; // public is also the default
}
const p = new Person();
console.log(p.name);

Private Members

Private members सिर्फ उस class के अंदर access किए जा सकते हैं जो उन्हें declare करती है, जो implementation details जैसे internal caches या helper state छिपाने के लिए उपयोगी है जिन्हें outside code को कभी सीधे touch नहीं करना चाहिए।

उदाहरण: Private Members

typescript
class BankAccount {
  private balance: number = 0;
  deposit(amount: number) {
    this.balance += amount;
  }
  getBalance() {
    return this.balance;
  }
}
const acc = new BankAccount();
acc.deposit(100);
console.log(acc.getBalance());

Protected Members

Protected members declaring class के अंदर और उन classes के अंदर access किए जा सकते हैं जो इससे inherit करती हैं, एक middle ground strike करते हुए जो उन values के लिए उपयोगी है जिन पर एक subclass को build करने की ज़रूरत हो लेकिन outside callers को नहीं देखना चाहिए।

उदाहरण: Protected Members

typescript
class Animal {
  protected sound: string = "...";
}
class Dog extends Animal {
  bark() {
    console.log(this.sound);
  }
}
new Dog().bark();

Modifiers को Combine करना

एक class अपने members में अलग-अलग access modifiers mix कर सकती है, एक छोटा public API expose करते हुए जबकि supporting fields और helper methods को private या protected रखते हुए, जो encapsulation का essence है।

उदाहरण: Combining Modifiers

typescript
class User {
  public name: string;
  private password: string;
  protected role: string;
  constructor(name: string, password: string, role: string) {
    this.name = name;
    this.password = password;
    this.role = role;
  }
}
const u = new User("Sam", "secret", "admin");
console.log(u.name);

सही Modifier चुनना

उन values के लिए public उपयोग करें जो object के accessible interface का हिस्सा हैं, implementation details के लिए private जिन पर कोई और code depend नहीं करना चाहिए, और protected उस किसी भी चीज़ के लिए जिसे एक future subclass extend या override करने की उम्मीद है।

उदाहरण: Choosing the Right Modifier

typescript
class Order {
  public id: number;       // part of the public interface
  private total: number;   // implementation detail
  protected status: string; // usable by subclasses
  constructor(id: number, total: number, status: string) {
    this.id = id;
    this.total = total;
    this.status = status;
  }
}
console.log(new Order(1, 50, "pending").id);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि private runtime पर data छिपाता है, जब यह सिर्फ compiler द्वारा check किया जाता है और value emitted JavaScript में अब भी visible है।
  2. एक protected member को class के बाहर से access करना, जैसे obj.sound, जो एक error है और सिर्फ class और इसकी subclasses में allowed है।
  3. एक constructor parameter में private जोड़ना यह realize किए बिना कि यह एक property भी बनाता है, या यह उम्मीद करना कि दूसरी classes इसे directly पढ़ सकेंगी।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.