166k views
2 votes
A class design requires that a particular member variable must be accessible by any subclasses of this class, but otherwise not by classes which are not members of the same package. What should be done to achieve this?

A. The variable should be marked public.
B. The variable should be marked private.
C. The variable should be marked protected.
D. The variable should have no special access modifier.
E. The variable should be marked private and an accessor method provided.

User Kadisha
by
7.4k points

1 Answer

5 votes

Final answer:

The member variable should be marked as protected to be accessible by subclasses and classes in the same package, but not by other external classes.

Step-by-step explanation:

To achieve the requirement that a member variable must be accessible by subclasses but not by classes which are not in the same package, the member variable should be marked as protected. This access modifier ensures that the member variable can be accessed by subclasses, regardless of the package they belong to, and also by classes in the same package, but not by other classes outside the package that are not subclasses.

User Vkostromin
by
8.3k points