aspose file tools*
The moose likes Beginning Java and the fly likes Protected class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected class" Watch "Protected class" New topic
Author

Protected class

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 23, 2001
Posts: 29
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.


Sharon
Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
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

Joined: May 08, 2001
Posts: 1932
I would only correct you on the C++ comment
Jason
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 01, 2001
Posts: 62
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

Joined: Aug 25, 2001
Posts: 10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected class
 
Similar Threads
Inner class
Reg. Inner classes : Valiveru's exam
inner class
Inner Class
inner class