aspose file tools*
The moose likes Java in General and the fly likes Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classes" Watch "Classes" New topic
Author

Classes

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

hi all,
This seems to be a very simple and straight forward,but i want a logic for the fact that why are protected classes not allowed while default classes are allowed as top level classes,even though protected is less restrictive as compared to default?

Thanks...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Please read Class access modifiers.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aniket S. Kulkarni
Ranch Hand

Joined: Jun 15, 2011
Posts: 87

default: If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. This access-level is convenient if you are creating packages.

protected: Fields, methods and constructors declared protected in a superclass can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

This may help you.


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
The original question was about why you can’t have a protected top-level class.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Classes in different packages cannot access classes that are protected. so it is meaningless to have the top level class protected. we already have default access to provide access for classes within same package
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classes