C# & .NET Framework OOP — Building Blueprints
💡
Exercise 34

Access Modifiers — Security Clearance Levels 20 XP Medium

Ctrl+Enter Run Ctrl+S Save

In every building, some areas are open to the public, some require employee ID, and some need top-secret clearance. C# access modifiers work the same way — they control who can see and use your class members.

This is the heart of encapsulation: you expose what's needed and hide everything else. It prevents external code from messing with your object's internal state and keeps your codebase maintainable.

class Building { public string Lobby = "Welcome!"; // anyone can access private string vaultCode = "X7-GAMMA"; // only this class protected string blueprint = "Structural"; // this class + subclasses internal string systems = "Online"; // same assembly only }
  • public — accessible from anywhere. The lobby: open to all visitors.
  • private — accessible only within the same class. The secret vault: only building management.
  • protected — accessible within the class and its derived classes. The blueprints: shared with branch buildings.
  • internal — accessible within the same assembly (project). Internal systems: available to other classes in your project, not outside.
  • protected internalprotected OR internal. Accessible to derived classes or same assembly.
  • private protectedprotected AND internal. Only derived classes within the same assembly.

Here's the golden rule: make everything as private as possible, then relax access only when needed. Start locked, open selectively.

class SecureBuilding { private string employeeId; // hidden from outside public SecureBuilding(string id) // public: anyone can create { employeeId = id; // set internally } public string VerifyId() // public method controls access { return $"ID verified: Employee #{employeeId}"; } // Outside code can't do: building.employeeId (it's private!) // They must use: building.VerifyId() — controlled access }

Notice the pattern: the data (employeeId) is private, but we provide a public method to interact with it safely. This is information hiding — one of OOP's most important principles.

If you don't specify a modifier, C# defaults to private for class members and internal for top-level classes. Always be explicit about your intent — don't rely on defaults.

class Demo { string secret; // private by default (class member) void DoStuff() { } // private by default } class TopLevel { } // internal by default (top-level class)
📋 Instructions
**Set Your Security Clearance!** 1. Create a `Building` class with: - A `public` method `GetLobbyMessage()` that returns `"Public lobby: Welcome!"` - A `private` field `employeeId` (string), set via constructor parameter - A `public` method `VerifyEmployee()` that returns `$"ID verified: Employee #{employeeId}"` - A `private` method `GetVaultAccess()` that returns `"Top Secret Data"` - A `public` method `RequestVaultAccess()` that prints `"Secret vault: Access requires clearance"` (it does NOT call `GetVaultAccess` — the point is the vault stays locked!) - An `internal` field `systemStatus` set to `"Online"` in the constructor 2. In `Main`: - Create a `Building` with `employeeId = "1234"` - Print the lobby message - Print the employee verification - Call `RequestVaultAccess()` - Print `$"Internal systems: {b.systemStatus}"`
The constructor should take a `string id` parameter and set `employeeId = id` and `systemStatus = "Online"`. `GetVaultAccess()` is private so it can't be called from Main — `RequestVaultAccess()` is the public-facing method that just prints the clearance message.
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.