Bird
Raised Fist0
C Sharp (C#)programming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does the protected access modifier mean in C#?

easy
A. Only the class itself and its subclasses can access the member.
B. Any code in the same assembly can access the member.
C. Only code outside the class can access the member.
D. The member is accessible everywhere without restriction.

Solution

  1. Step 1: Understand the meaning of protected

    The protected modifier restricts access to the class itself and any classes that inherit from it.
  2. Step 2: Compare with other access levels

    Unlike public or internal, protected hides members from outside classes except subclasses.
  3. Final Answer:

    Only the class itself and its subclasses can access the member. -> Option A
  4. Quick Check:

    Protected = class + subclasses access [OK]
Hint: Protected means class and subclasses only [OK]
Common Mistakes:
  • Confusing protected with public
  • Thinking protected allows access from unrelated classes
  • Mixing protected with internal or private
2.

Which of the following is the correct way to declare a protected method named Calculate in C#?

?
easy
A. internal void Calculate() { }
B. private void Calculate() { }
C. public void Calculate() { }
D. protected void Calculate() { }

Solution

  1. Step 1: Recall protected method syntax

    In C#, the keyword protected is used before the return type to declare a protected method.
  2. Step 2: Check each option

    protected void Calculate() { } uses protected void Calculate(), which is correct syntax for a protected method.
  3. Final Answer:

    protected void Calculate() { } -> Option D
  4. Quick Check:

    Protected method syntax = protected + return type [OK]
Hint: Protected methods start with 'protected' keyword [OK]
Common Mistakes:
  • Using private or public instead of protected
  • Omitting the return type
  • Placing protected after the method name
3.

Consider the following code:

class Parent {
    protected int number = 5;
}

class Child : Parent {
    public int GetNumber() {
        return number;
    }
}

class Program {
    static void Main() {
        Child c = new Child();
        Console.WriteLine(c.GetNumber());
    }
}

What will be the output when this program runs?

medium
A. 0
B. 5
C. Compilation error
D. Runtime error

Solution

  1. Step 1: Understand protected member access in subclass

    The number field is protected, so the subclass Child can access it directly.
  2. Step 2: Trace the program output

    The GetNumber method returns number which is 5, so Console.WriteLine prints 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Protected field accessed in subclass = 5 [OK]
Hint: Protected members accessible in subclass methods [OK]
Common Mistakes:
  • Thinking protected means inaccessible outside parent
  • Expecting compilation error due to access
  • Confusing protected with private
4.

Identify the error in this code snippet:

class Base {
    protected int value = 10;
}

class Other {
    void Show() {
        Base b = new Base();
        Console.WriteLine(b.value);
    }
}
medium
A. Class Other must inherit Base to access value.
B. Missing semicolon after declaration.
C. Cannot access protected member from unrelated class instance.
D. No error, code runs fine.

Solution

  1. Step 1: Check access of protected member from unrelated class

    The class Other does not inherit from Base, so it cannot access value which is protected.
  2. Step 2: Understand protected access rules

    Protected members are accessible only within the class and its subclasses, not from unrelated classes.
  3. Final Answer:

    Cannot access protected member from unrelated class instance. -> Option C
  4. Quick Check:

    Protected access = class + subclasses only [OK]
Hint: Protected not accessible from unrelated classes [OK]
Common Mistakes:
  • Assuming protected is like public
  • Thinking inheritance is not required
  • Ignoring access modifier rules
5.

You want to create a base class Vehicle with a protected field speed. You also want a subclass Car that can set and get this speed, but no other class should access it directly. Which code snippet correctly implements this?

hard
A. class Vehicle { protected int speed; } class Car : Vehicle { public void SetSpeed(int s) { speed = s; } public int GetSpeed() { return speed; } }
B. class Vehicle { public int speed; } class Car : Vehicle { } // Any class can access speed directly
C. class Vehicle { private int speed; } class Car : Vehicle { public int GetSpeed() { return speed; } }
D. class Vehicle { internal int speed; } class Car : Vehicle { } // speed accessible only in same assembly

Solution

  1. Step 1: Use protected field in base class

    The field speed is declared protected in Vehicle so only Vehicle and subclasses can access it.
  2. Step 2: Provide public methods in subclass to access speed

    The subclass Car has public methods SetSpeed and GetSpeed to safely access the protected field.
  3. Step 3: Check other options for access control

    class Vehicle { public int speed; } class Car : Vehicle { } // Any class can access speed directly uses public field, which allows all classes to access speed directly. class Vehicle { private int speed; } class Car : Vehicle { public int GetSpeed() { return speed; } } uses private field, so subclass cannot access it. class Vehicle { internal int speed; } class Car : Vehicle { } // speed accessible only in same assembly uses internal, which restricts access to assembly, not subclasses.
  4. Final Answer:

    class Vehicle { protected int speed; } class Car : Vehicle { public void SetSpeed(int s) { speed = s; } public int GetSpeed() { return speed; } } -> Option A
  5. Quick Check:

    Protected field + subclass methods = correct encapsulation [OK]
Hint: Protected field with public subclass methods controls access [OK]
Common Mistakes:
  • Using public field exposing speed to all
  • Using private field inaccessible to subclass
  • Confusing internal with protected