Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is package visibility given priority over subclass visibility?

This answer shows Java's visibility modifiers and their meaning:

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  y    |    y    |    y     |   y
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    y     |   n
————————————+———————+—————————+——————————+———————
no modifier |  y    |    y    |    n     |   n
————————————+———————+—————————+——————————+———————
private     |  y    |    n    |    n     |   n

My question is, why does allowing visibility to all subclasses imply that you must give visibility to all other classes in your package? In other words, why did the Java creators make it like this, as opposed to:

Modifier    | Class | Subclass | Package | World
————————————+———————+—————————-+——————————+———————
public      |  y    |    y    |    y     |   y
————————————+———————+—————————+——————————+———————
no modifier |  y    |    y    |    y     |   n
————————————+———————+—————————+——————————+———————
protected   |  y    |    y    |    n     |   n
————————————+———————+—————————+——————————+———————
private     |  y    |    n    |    n     |   n
like image 373
morsecoder Avatar asked Jun 17 '15 15:06

morsecoder


People also ask

What is package visibility?

The limited package visibility reduces the number of apps that appear to be installed on a device, from your app's perspective. This filtering behavior helps minimize the amount of potentially sensitive information that your app doesn't need in order to fulfill its use cases, but that your app can still access.

How should the visibility of variables and methods be declared?

Methods and variables are always visible within a declaring class itself, so the table doesn't address that scope.

What is the default visibility for a class?

Default visibility allows a variable or method to be seen by all methods of a class or other classes that are part of the same package. A package is a group of related classes. For now, default visibility means about the same thing as public visibility.


2 Answers

A package is supposedly written and maintained by the same team; it's ok to have looser access control within a package, assuming the programmer have an intimate knowledge of all the code.

A subclass is often written by someone else; a stricter constraint on API is better.

like image 88
ZhongYu Avatar answered Oct 16 '22 18:10

ZhongYu


Because the other way round like you suggest doesn't make sense in practice (which I admit isn't that obvious).

Suppose you have a package, containing a base class intended for extension by a client (third party, other team etc.), as well as other classes interacting with the base class (in the same package). Thats a pretty common case.

Now, your base class may contain members/methods intended only for use by the code supplied in the package, but that are intended to be inaccessible to the client extending the class.

Thats simple with package private (default) modifier as it is now, but it would be impossible if you switched the strength. You would lose the ability to hide things from your child classes while still making them available to your companions in the package.

like image 35
Durandal Avatar answered Oct 16 '22 18:10

Durandal