File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 207

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: 19541
    
  16

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: 36453
    
  15
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
 
Similar Threads
Top level Class can be private,protected?
inner class
Class access modifiers
enums and protected modifier
private and protected class - why not?