Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no sub-class visibility modifier in Java?

On more than one occasion I have found myself desiring a variable visibility that is not possible in Java. I wanted certain members to be visible within their own class and within any sub-classes, but not to the rest of the package or to the rest of the world. In other words, I wanted this:

Modifier        Class     Package   Subclass  World sub-class       Y         N         Y         N 

However, the designers of Java only gave me this:

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 

The typical case when I want something like this is when creating an abstract class. Sometimes I find the abstract parent needs access to certain members, but concrete children do as well. I can give them this access by making the members protected, but that opens up accessibility to the rest of the package when I don't really want to.

To be fully clear, I know such a modifier is not possible in Java. My question is why is such a modifier not included in Java? It seems (to me) to be a more natural visibility level than either protected or the default. Is the reason just along the lines of it not being sufficiently important to be included, or is it more related to possible side effects that I haven't considered?

like image 726
Michael McGowan Avatar asked Mar 14 '11 14:03

Michael McGowan


People also ask

What is the visibility of a class without an access modifier?

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

What is no modifier class in Java?

Default Access Modifier - No KeywordA variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

How many visibility modifiers are there in Java?

Simply put, there are four access modifiers: public, private, protected and default (no keyword).


2 Answers

I suppose they want to avoid the added complexity by having a non-linear access hierarchy.

You should have control over your package, so simply don't call these protected methods there.

(By the way, protected is not quite the same as sub-class and package, as non-static protected methods (if not in the same package) can't be called on arbitrary objects of the declaring class, but only on objects of the subclass the code is in. (You can see this on Object.clone(), which can only be called by the class whose object is being cloned.))

like image 187
Paŭlo Ebermann Avatar answered Oct 20 '22 08:10

Paŭlo Ebermann


Being-in-the-same-package is simply considered a closer relationship than being-a-subtype-of.

Why?

You typically control all source code of the package you're developing(*), so you at least have the possibility to avoid making erroneous calls.

You do not control all code that extends your classes. (Anyone can extend your class.) This means that package private access plays a more important role.


*) But hey, I an start any source file with package com.yourpackage; so you don't control all code in your package! Well, yes, but a) you're not really supposed to do that, and b) it can be prevented by sealing the packages.

like image 43
aioobe Avatar answered Oct 20 '22 08:10

aioobe