wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on protected access modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on protected access modifier" Watch "Question on protected access modifier" New topic
Author

Question on protected access modifier

Saroja Eshwar
Greenhorn

Joined: Oct 26, 2007
Posts: 10
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

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
abhishek pendkay
Ranch Hand

Joined: Jan 01, 2007
Posts: 184
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


The significant problems we face cannot be solved by the same level of thinking which created them – Einstein
SCJP 1.5, SCWCD, SCBCD in the making
Saroja Eshwar
Greenhorn

Joined: Oct 26, 2007
Posts: 10
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

Joined: Aug 29, 2006
Posts: 132
Saroja its a good question...

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



Perform for today. Adapt for tomorrow.
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
A class of the top level can not be marked with other modifiers besides default and public.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Gaurav Minhas
Greenhorn

Joined: Oct 21, 2007
Posts: 2
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

Joined: Mar 31, 2005
Posts: 3
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on protected access modifier
 
Similar Threads
Protected Class?
protected class ???
Java Object class
protected class
protected class?