Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected access modifier

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ask my self about protected modifier.
Here's what is said in Complete Java 2 Certification Guide from Philipp Heller and Simon Roberts:

only variables and methods may be declared protected


So, no of course, top level classes nor inner classes.
But here's what can be read in danchisholm mock exam on declaration.

If a class C is declared as a member of an enclosing class then C may be declared using no access modifier or any of the three access modifiers, private, protected, or public. However, if class C is not declared as a member of an enclosing class then C may be declared with no access modifier or using the access modifier "public". The other two access modifiers, private and protected, are not applicable to any class that is not a member class.


So my question is the following:
May a member class be declared protected ?
Thanks in advance,
Cyril.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cyril
Top level classes can't be protected. Member classes can be. But I guess that a better way to make yourself sure is by trying a piece of code that does that.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member classes can be final, abstract, protected, private, public, static, strictfp.
Method-local inner classes can be either abstract or final.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A member class of a class may be declared protected.
A member class of an interface is implicitly public.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my god, this is really funny. I just posted the same exact question about the exact same line in that book! I just did a double-take when I saw the topic.
Well, looks like I'm off to delete my post. Yes, clearly protected is allowed, I tried it, and it makes sense that it would be. In the book for other modifiers they say it can be used on "classes methods and variables", and so on. I guess maybe at this chapter, which is before nested classes, they think of "classes" as top-level only.
P.S. Read the last sentence on page 95. They state that classes are considered top-level in their chart on the following page
[ July 04, 2003: Message edited by: Brian Joseph ]
 
I AM MIGHTY! Especially when I hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic