Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected class

 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to declare a class as protected?
I tried it for an outer class and it gave me the error message:
"Class or interface declaration expected."
When I tried it for an inner class, no problem. Why is it the access modifier 'protected', allowed for inner classes and not outer classes?
Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I don't think that you can delclare a class protected unless it's an inner class. You can't delcare it private. You can delcare it final. You can't delcare it static! Please correct me if I'm wrong. I Didn't look this up.
 
Sharada Kumaran
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. A Package level class (not inner or anonymous), cannot be declared anything other than public, or else they mustn't be declared. The default accessibility will be friendly.
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mathias, Sharada.
From Bill Brodgen's Exam Cram(Java 2):
"Classes cannot be protected, private, native or synchoronized."
I don't agree with you Sharada. A class can have the default access modifier which enables package-level accessibility. 'friendly' is NOT a Java keyword. It's a C++ keyword.
Do correct me if I'm wrong in any of the above.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would only correct you on the C++ comment
Jason
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sharada! I think what he meant was that if a class doesn't have any accessmodifier then the class is default. You can maybe use the word friendly. Freindly is not a java keyword so you are correct. default is a java keyword but you do not use it in a class declaration.
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a she...
Yeah, Mathias, that's what I meant. I tried using friendly in place of public, but it gave me a compile-time error. Thanks again for everyone's help.
 
Christopher Knoepfle
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about the purpose of access modifiers:
At the outer class level, the only question regarding access that
you have to ask is: can this class be accessed from outside of its package? If yes, stick public in front of it. If no, it gets default (package level) access and doesn't need a modifier.
Once you've determined the outer class level access, you have to ask how accessible the class's methods and variables should be? Your answer to the first question will affect your choices for the second.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic