Which access modifier should you use?

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?

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



Leave a Reply 8

Your email address will not be published. Required fields are marked *


jeff

jeff

it should be protected, isn’t it? because private variable cannot be accessed by derived class

aaaa

aaaa

“You need to restrict the availability of” so it is private

aaaa

aaaa

my bad, thats protected

vstaykov

vstaykov

Yes, the right answer should be “protected”.

Serge

Serge

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;}”

Gena Beamon

Gena Beamon

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.)

jj

jj

“Protected” in the correct answer.

Nikesh

Nikesh

Protected. Inherited class will require protected access modifier