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.
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).]
Joined: Jan 30, 2000
Joined: Jul 28, 2000
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).]