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

Question on protected access modifier

 
Saroja Eshwar
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't we make a class protected??

like protected class Test{
}

I know we cannot make a class private but why not protected??
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your info, inner and nested classes can be either private or protected. The restriction you're talking about only applies to top-level classes.

A top-level class can only have public or default visibility. Since you understand why a top-level class cannot be private, try applying the same reasoning to figure out why there's no point in allowing a top-level class to be protected, when it can already have default visibility.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can inner class protected (or private) the restriction applies to top level classes because it simply does not make any sense... think about it theres nothing that you will acheive by making a class protected
 
Saroja Eshwar
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have default access for class.. which means we can see that particular class in same package to which it belongs to...

Similarly public class can be seen in any packages...

So if i want my class to be visible in other packages but not to all the classes mean to say i want that class to be visible to subclassess then we could have used this protected modifier rite??

I know there is no use of protected classess but even then why we dont have that... Is there any specific reason for it??
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saroja its a good question...

yes, if i want that only childs of that class will access it. it may be my desire.

 
camilo lopes
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class of the top level can not be marked with other modifiers besides default and public.
 
Gaurav Minhas
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always remember that Protected Members are accessed through Inheritance and not reference......
It does not make much sense to mark a Top Level class Protected, Public and default do the Job well.
You use protected access modifiers for members..... which actually need to be inherited.

[ October 31, 2007: Message edited by: Gaurav Minhas ]
[ October 31, 2007: Message edited by: Gaurav Minhas ]
 
Marinho Tobolla
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know it too, why it isn�t possible.

I know, that lots of people tell me, that nobody wants a protected class (Apart from inner classes). Well i don�t know a good reason for wanting it, too.

But that can�t be the reason why it isn�t possible.

Protected means visible to the package and thru inheritance - not visible by reference (at least in other packages).

Is it possible, that the import or the extends statements are seen as reference ?
If that would be true, than it wouldn�t make any sense to have a protected class, cause it would be the same as default, since it wouldn�t be visible in other packages.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic