my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Top Level Class 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 "Top Level Class" Watch "Top Level Class" New topic
Author

Top Level Class

Basu Patel
Ranch Hand

Joined: May 28, 2000
Posts: 60
I require a justification for why a top level class can not be protected, and would not compile?...thanx for the help!
Basu


<BR>Contact Me<BR> <A HREF="mailto:basu_patel@usa.net" rel="nofollow">basu_patel@usa.net</A> <P>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It won't compile because the JLS says that top-level classes can only be public or default access, not private or protected. That's all the reason the compiler needs.
Now, why does the JLS say this? My best guess is that no one ever imagined a situation where they would be useful. It seems like they'd lead to an odd chicken-or-egg situation: Imagine that protected top-level classes are allowed, and you have a package p1 with a protected class p1.A. And you have another package 02, where you're writing a (normal) class p2.B. Normally you can't refer to p1.A from within p2.B, because p1.A is protected. So OK, you need p2.B to extend p1.A in order to have access to it. But you can't extend it unless you already have access. Or can you? I suppose the JLS could have let this be legal, but it seems pretty weird to me, and I can't imagine any reason why we'd need it. I hope that works for you too, otherwise I don't really have a better ansewr.

"I'm not back." - Bill Harding, Twister
Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850
Hi Jim,
could you please clarify what you mean when you say
"So OK, you need p2.B to extend p1.A in order to have access to it. But you can't extend it unless you already have access. Or can you? "
you mean to say this in code.


[This message has been edited by Rahul Mahindrakar (edited August 01, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I meant:

Rahul Mahindrakar
Ranch Hand

Joined: Jul 28, 2000
Posts: 1850

Jim,
don't you think that if the above code were legal then one could have a class that one can only extend in other packages but one cannot have refer to in classes as in B above. Won't this be useful?. the public modifier makes a class accessible as well as subclassable.

[This message has been edited by Rahul Mahindrakar (edited August 02, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Top Level Class