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?
A.
Internal
B.
Protected
C.
Private
D.
Public
it should be protected, isn’t it? because private variable cannot be accessed by derived class
“You need to restrict the availability of” so it is private
my bad, thats protected
Yes, the right answer should be “protected”.
it’s confusing, but if you do a shortcut like “restrict availability to the base class and derived classes” – means PRIVATE. A private member can be accessed within the body of the class using method like “public GetAccessCount() {return accessCount;}”
A new class is defined by using the keyword class. Here, the Rectangle class has two data fields, length and width. These fields are defined by using the access modifier private. An access modifier specifies what region of the code will have access to a field. For example, the access modifier public will not limit access, but the access modifier private will limit access within the class in which the field is defined.
The usual programming pattern is that all the data fields of a class should be declared
private, and that access to these private fields should be via public properties that check the data values for validity. (2012 John Wiley & Sons, Software Development Fundamentals, Exam 98-361, page 34 & 37.)
“Protected” in the correct answer.
Protected. Inherited class will require protected access modifier