How do the four access levels public, protected, private, and package-private differ in common OO languages?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

How do the four access levels public, protected, private, and package-private differ in common OO languages?

Explanation:
Visibility of members is controlled by access modifiers, determining who can use them. Public means you can access the member from anywhere. The option described as protected reflects how the modifier works in common OO languages like Java: it allows access to the declaring class and its subclasses, and also to other classes in the same package. That combination means protected is broader than private but not universal like public, since outside packages you only get access if you’re in a subclass. Private is the most restrictive, visible only inside the class that declares it. Package-private (the default when no modifier is specified) grants access to all classes within the same package but blocks access from outside the package. This pattern matches the typical Java semantics, where omitting a modifier yields package-private, and protected includes package access plus subclass access.

Visibility of members is controlled by access modifiers, determining who can use them. Public means you can access the member from anywhere. The option described as protected reflects how the modifier works in common OO languages like Java: it allows access to the declaring class and its subclasses, and also to other classes in the same package. That combination means protected is broader than private but not universal like public, since outside packages you only get access if you’re in a subclass. Private is the most restrictive, visible only inside the class that declares it. Package-private (the default when no modifier is specified) grants access to all classes within the same package but blocks access from outside the package. This pattern matches the typical Java semantics, where omitting a modifier yields package-private, and protected includes package access plus subclass access.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy