PrepAway - Latest Free Exam Questions & Answers

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?

PrepAway - Latest Free Exam Questions & Answers

A.
Internal

B.
Protected

C.
Private

D.
Public

8 Comments on “Which access modifier should you use?

  1. Serge says:

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




    1



    0
  2. Gena Beamon says:

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




    0



    1

Leave a Reply