0
0
C Sharp (C#)programming~10 mins

Protected access modifier in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Protected access modifier
Define Base Class with protected member
Create Derived Class inheriting Base
Access protected member inside Derived
Use Derived object to call method accessing protected member
Protected member NOT accessible outside inheritance
END
Shows how a protected member is accessible inside derived classes but not outside.
Execution Sample
C Sharp (C#)
class Base {
  protected int number = 42;
}
class Derived : Base {
  public int GetNumber() { return number; }
}
Defines a base class with a protected number and a derived class that accesses it.
Execution Table
StepActionEvaluationResult
1Create Base objectBase.number is protectedCannot access number outside Base or Derived
2Create Derived objectDerived inherits Base.numberDerived.number accessible inside Derived
3Call Derived.GetNumber()Returns protected numberReturns 42
4Try access Derived.number outside classAccess deniedCompilation error
5EndNo further accessExecution stops
💡 Protected member is accessible only within Base and Derived classes, not outside.
Variable Tracker
VariableStartAfter Derived CreationAfter GetNumber CallFinal
number42 (in Base)42 (inherited by Derived)42 (returned by GetNumber)42
Key Moments - 2 Insights
Why can't we access 'number' directly from a Derived object outside the class?
Because 'number' is protected, it is only accessible inside Base and classes derived from Base, not from outside code (see execution_table step 4).
How does Derived class access the protected member 'number'?
Derived inherits 'number' from Base and can access it directly inside its methods (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does Derived.GetNumber() return at step 3?
A42
B0
CCompilation error
DCannot access
💡 Hint
Check execution_table row with Step 3 showing the returned value.
At which step does the code fail to access the protected member from outside the class?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look for the step mentioning 'Compilation error' in execution_table.
If 'number' was public instead of protected, what would change in the execution table?
AStep 3 would fail
BStep 2 would fail
CStep 4 would succeed accessing number
DNo change
💡 Hint
Think about accessibility from outside the class in step 4.
Concept Snapshot
protected member:
- Accessible inside class and derived classes only
- Not accessible from outside objects
- Allows controlled inheritance access
- Use to hide details but allow subclass use
Full Transcript
This example shows how the protected access modifier works in C#. A base class defines a protected variable 'number'. A derived class inherits from the base and can access 'number' inside its methods. When we create an object of the derived class and call its method, it returns the protected number. However, trying to access 'number' directly from outside the classes causes a compilation error. This demonstrates that protected members are accessible only within the class and its subclasses, not from outside code.