• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on protected access modifier

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class of the top level can not be marked with other modifiers besides default and public.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic