It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Why can't a class be protected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't a class be protected?

J New
Greenhorn

Joined: Jun 27, 2008
Posts: 5
As a protected member of a class is available to non sub-classes in the same package(which signifies they(protected members) are visible),why can't a class be protected?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
First and foremost, someone is definately going to say something about your name, it needs to be your real name, or atleast sound like one lol.

and secondly, I dont know why you would want your class protected, protected is mainly a modifier for inheritance and allowing sub-classes to use those protected methods and variables.

Justin Fox


You down with OOP? Yeah you know me!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

"javaNewBie", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J New
Greenhorn

Joined: Jun 27, 2008
Posts: 5
I asked because its not C++,where a class doesn't have a access specifier.As Java added this feature,so my question is valid.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I never said your question wasn't valid, all I asked is why you would want a 'CLASS' protected? I don't think you would want to make a class all in itself protected, but maybe and instance of that class in another class.


Justin
[ June 27, 2008: Message edited by: Justin Fox ]
J New
Greenhorn

Joined: Jun 27, 2008
Posts: 5
It's not that i want.I am just curious to know about this feature of Java.Also, i am a bit surprised that a protected member can be accessed in a non sub-class,unlike C++.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"new Bie",
Please check your private messages regarding an important administrative matter.
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
J New
Greenhorn

Joined: Jun 27, 2008
Posts: 5
I have changed my name Sir.Why don't you see it?
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
I still doubt your name is "J New".

The short answer is "the spec doesn't allow it".

I would return the question with: what would a protected class mean?


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
A class can be protected. But a top-level class can't. This has come up several times recently. Like here, but I wouldn't take any notice of what THAT person replying said.

And, "J New" you are on your 3rd warning about names.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
. . . and lots of people think "protected" means the same in Java as it does in C++. It doesn't.
Sita Ram Pothula
Greenhorn

Joined: Jul 16, 2009
Posts: 2
The possible access modifiers for any top level class are either public or no modifier (package). Apart from this you can make the class abstract or final depending on the need. One of the reason could be they do not serve any purpose

Protected and private are very much acceptable modifiers, but they are applicable to inner classes.

Unline in C++, java inhenritance is always public. There is no mechanism to specify the modifier during inheritance the way you do it in C++.

Hope I answered your query.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Welcome to JavaRanch

Good answer but after a year, maybe a bit late.
Michael Smithers
Greenhorn

Joined: Nov 05, 2009
Posts: 1
Campbell Ritchie wrote:Welcome to JavaRanch

Good answer but after a year, maybe a bit late.


This is a stupid rule. It's very useful for people to update zombie threads because everyone searching for an answer will find it instead of posting a new thread.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

If you want to discuss this rule (actually more a guideline), please do this in our Ranch Office forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: Why can't a class be protected?