File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classes" Watch "Classes" New topic


Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 235

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?


Rob Spoor

Joined: Oct 27, 2005
Posts: 20183

Please read Class access modifiers.

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

Joined: Oct 13, 2005
Posts: 45277
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:
subject: Classes
It's not a secret anymore!