This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 215

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: 19654
    
  18

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: 38025
    
  22
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
 
Consider Paul's rocket mass heater.
 
subject: Classes
 
Similar Threads
private and protected class - why not?
enums and protected modifier
Class access modifiers
inner class
Top level Class can be private,protected?