Recall & Review
beginner
What does the
protected access modifier mean in C#?The <code>protected</code> access modifier allows a class member to be accessible within its own class and by derived classes (subclasses), but not from other unrelated classes.Click to reveal answer
beginner
Can a <code>protected</code> member be accessed from an instance of the class outside the class hierarchy?No, <code>protected</code> members cannot be accessed from outside the class or its subclasses, even if you have an instance of the class.Click to reveal answer
beginner
Example: What will happen if you try to access a
protected field from a non-derived class?You will get a compile-time error because <code>protected</code> members are not visible outside the class and its subclasses.Click to reveal answer
beginner
How does
protected differ from private and public?<code>private</code> means only the class itself can access the member.<br><code>protected</code> means the class and its subclasses can access it.<br><code>public</code> means anyone can access it.Click to reveal answer
intermediate
Why use
protected instead of private?Use
protected when you want to hide members from the outside world but still allow subclasses to use or modify them. It helps with controlled inheritance.Click to reveal answer
Which classes can access a
protected member in C#?✗ Incorrect
protected members are accessible only within the declaring class and its derived classes.Can a
protected member be accessed by an instance of a derived class from outside the class?✗ Incorrect
You can access
protected members from within the derived class code, but not from outside using an instance.What happens if you try to access a
protected member from a non-derived class?✗ Incorrect
Accessing
protected members outside the class hierarchy causes a compile-time error.Which access modifier allows access from anywhere?
✗ Incorrect
public members are accessible from any code.Why might a developer choose
protected over private?✗ Incorrect
protected allows subclasses to access members, unlike private which restricts access to the declaring class only.Explain in your own words what the
protected access modifier does and when you would use it.Think about who can see the member and why inheritance matters.
You got /4 concepts.
Describe the difference between
private, protected, and public access modifiers with simple examples.Imagine who can open a door labeled private, protected, or public.
You got /4 concepts.