Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't a class be protected?

 
J New
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"javaNewBie", please check your private messages for an important administrative matter.
 
J New
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"new Bie",
Please check your private messages regarding an important administrative matter.
-Ben
 
J New
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed my name Sir.Why don't you see it?
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and lots of people think "protected" means the same in Java as it does in C++. It doesn't.
 
Sita Ram Pothula
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Good answer but after a year, maybe a bit late.
 
Michael Smithers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to discuss this rule (actually more a guideline), please do this in our Ranch Office forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic