How to determine the level of access of the elements of a class?

Asked

Viewed 138 times

1

This is a question of the Secitec 2018 competition for the position of computer teacher. I appealed to this question because there are no qualifiers, but access modifiers such as: private, protected and public. The reserved word "package" is not an explicit access modifier according to Deitel & Deitel. What alternative in this case would be the correct one? Then the question follows.

Encapsulation is one of the basic principles used in object-oriented programming, which makes it possible to restrict access to variables and methods a class. To determine the access level of elements of a class are used qualifiers, two of them are characterized below.

(I) It is the most rigid level at which only the methods of the own class have access to variables and other methods.

(II) Is the level at which variables and methods can be accessed only by other classes belonging to to the same package.

The qualifiers (I) and (II) are called, respectively:

A) public and package

B) public and protected

C) private and package

D) private and protected

1 answer

1


Depending on the interpretation of the examiner’s bench may be alternative C or question reversed. In fact, the term package does not apply to the access modifier but to the access level (package-protected, or package-level protected). The fact of calling qualifier modifier is subject to the interpretation of the bench, because it is possible to understand one another, although the usual term is modifier.

  • Would the D alternative not be correct? The protected modifier defines, for the element (variable or method), the package level access, correct?

  • As the term "only" was used in affirmation II, I think protected is excluded, because a subclass in another package could not access since "only" classes of the same package would be allowed. Already with protected this would be possible.

  • Correct. Thank you very much!

  • If the answer was useful to you, you can accept it by clicking on the "v" in the upper left corner of it, below the polling arrows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.