Software Development Fundamentals — Question 16
You are designing a class for an application. You need to restrict the availability of the member variable accessCount to the base class and to any classes that are derived from the base class.
Which access modifier should you use?
Answer options
- A. Protected
- B. Private
- C. Public
- D. Friend
Correct answer: B
Explanation
The correct answer is B, Private, because it restricts access to the member variable only to the class in which it is declared. While Protected would allow derived classes access, it does not meet the requirement of restricting access to just the base class and its child classes. Public would allow access from any class, and Friend is not applicable in this context as it allows access from specified classes rather than restricting it.