permaculture playing cards*
The moose likes Java in General and the fly likes protected access of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "protected access of a class" Watch "protected access of a class" New topic
Author

protected access of a class

Rakesh Gulati
Greenhorn

Joined: Jul 06, 2000
Posts: 4
Its fine you can't make a top level class as private but why can't protected(though you can make a class with default accessibility and default accessibility is less accessible than protected)
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
default accessibility is more than protected I think.
Gopinath Rajgopal
Ranch Hand

Joined: Jun 26, 2000
Posts: 31
No, default access is not more than protected access. You can remember this using the following:
protected access = (default access, i.e access within same package) + (access to subclasses in other packages)
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
I think protected methods are only accessible to its subclasses, but default methods are accessible any where within the same package. But you are also right.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Rakesh,
first of all i would like to tell u that u have indeed raised very good question. It really boggled me. u r logic is correct. the access specifiers based on accessibility IMO are
public
protected
default
private
however a top level class can only be of default or public class.
I checked out the JLS version 2 8.1.1 on this. I quote
"Not all modifiers are applicable to all kinds of class declarations. The access modifier public pertains only to top level classes and to member classes. The access modifiers protected and private pertain only to member classes within a directly enclos-ing class declaration.
here is section 7.6 which clearly states
"It is a compile time error if a top level type declaration contains any one of the following access modifiers: protected, private or static."
so as per the JLS the matter stands closed
but as to why? the question is still open.
Regds.
Rahul.

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What would <code>protected</code> mean when applied to a top-level class?
<code>protected</code> members are accessible to subclasses and other classes in the same package and <code>public</code> members are accessible anywhere the class is accessible, so there is a clear progression of access modifiers with respect to members.
<code>public</code>, when applied to a class, means the class is accessible anywhere its package is accessible. Drawing a parallel with the case of <code>protected</code> members, the only meaning I can see of a <code>protected</code> class would be that the class is accessible to "subpackages" (derived from the class's package), but there is no such thing as inheritance of packages. Containment isn't inheritance.
Note that inner classes can be <code>protected</code> because they are members and so are subject to inheritance (controlled by their access modifier) when their enclosing class is extended.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected access of a class