File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Top Level Class

 
Basu Patel
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I require a justification for why a top level class can not be protected, and would not compile?...thanx for the help!
Basu
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant:

 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic