File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How come public and friendly(default) access is allowed for top level class but not Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How come public and friendly(default) access is allowed for top level class but not " Watch "How come public and friendly(default) access is allowed for top level class but not " New topic
Author

How come public and friendly(default) access is allowed for top level class but not

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
protected or private
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

well, to be blunt and un-helpful
1- "because Sun made it that way."
But... to give a little bit more explanation:
2- Cause the JLS says so
(real helpful there huh?)
How's this one -->
3- it wouldn't make sense to make a top-level class as private -- how would you access it to run it? No other class could see it!
4- There's sorta no point to mark a top-level class as protected -- protected access gives you the same access as default (package/friendly) but you can also see it from a sub-class. But how would that work when it's a top-level class that you're referring to?


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Originally posted by Jessica Sant:
protected access gives you the same access as default (package/friendly) but you can also see it from a sub-class. But how would that work when it's a top-level class that you're referring to?


if a top-level class had protected access couldn't another class from another package then extend it and thus access it?
ps- interesting poem you have there in your sig jessica!


giddee up
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem is - in order to extend a class, the class definition must already be visible to you. (E.g. you couldn't extend a private class, period.) Before a protected class can be visible to you, you have to extend it. Which comes first? I don't really see a logical way around this problem, and moreover I don't see any real benefit to having a protected class anyway. You can always make all the methods, constructors and fields protected or private, while making the class itself public - this would prevent any outside code from doing anything significant with the class. So there's no real need for Java to allow protected classes anyway, and they'd just cause confusion.
And as Jessica said, there's no way to make use of a private top-level class - so it makes even more sense to ban this possiblity.


"I'm not back." - Bill Harding, Twister
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
thanks Jim, Jessica, good words. interesting question!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How come public and friendly(default) access is allowed for top level class but not