This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.
Joined: Aug 01, 2001
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.
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.
Joined: Aug 01, 2001
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.
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.