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

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

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