File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes cannot be marked as protected?????

 
dpy learn
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question in K&B Book - protected class
4. Which two of the following are legal declarations for nonnested classes and interfaces?
(Choose two.)
A. final abstract class Test {}
B. public static interface Test {}
C. final public class Test {}
D. protected abstract class Test {}
E. protected interface Test {}
F. abstract public class Test {}
df

he give answer is:
C, F. Both are legal class declarations.
A is wrong because a class cannot be abstract and final´┐Żthere would be no way
to use such a class. B is wrong because interfaces and classes cannot be marked as static.
D and E are wrong because classes and interfaces cannot be marked as protected.

???Classes cannot use protected.
 
Purujit Saha
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nonnested class can never be
private, protected & static
 
A Kumar
Ranch Hand
Posts: 980
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

For a non-nested class/interface

class:

default,public,abstract,final

interface

default,public,abstract

Regards
 
dpy learn
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all
 
Shanel Jacob
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

1) Regarding the discussion above, while a top level class can be Default, Public, Abstract or Final (but not both of course): Can someone explain why a top level class "can never be private, protected & static".

2) A member/nested class can have the modifiers of top level class: Default, Public, Abstract or Final + "private, protected & static". Is this assumption correct?

Thank you.
 
mmg
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) A class can't be:
- private because then you wouldn't be able to use it
- protected because this would mean that subclasses can use it. From the moment you subclass it you would be able to use it, so that doesn't make alot of sence...

2) yes, this is correct
 
Shanel Jacob
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok makes more sense that top level class cannot be marked Private, otherwise you cannot use it.

However, what's wrong with marking top level class Protected? That just means you are restricting subclasses outside the package from accessing it eh? What's wrong with that?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic