jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Why can't a class be declared private or protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why can Watch "Why can New topic
Author

Why can't a class be declared private or protected

saloni pai
Greenhorn

Joined: Jul 04, 2002
Posts: 2
Can anyone please tell me the exact funda/reason as to why Java class can only be declared publec or default access, but not private or protected.
(I am not considering inner classes, just a normal class)... The compiler flags an error, but i would like to know the exact reason.
Thanks !
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Why would you want to declare a class as private or protected? How would you access it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
saloni pai
Greenhorn

Joined: Jul 04, 2002
Posts: 2
I know that the class cannot be accessed by using the modifier private or protected.
But during the compilation time itself, i get the error message that the modifier is not allowed.
Granted that a private class cannot be accessed, but what about a protected class. i could always subclass a protected class and use it then..
So, i require the exact reason..
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by ujsm:
I know that the class cannot be accessed by using the modifier private or protected.
But during the compilation time itself, i get the error message that the modifier is not allowed.

It's not allowed because it would be inane to do so. It helps find problems faster, if for example you refactor a private nested class to a standalone one and forget to change the access modifier.

Granted that a private class cannot be accessed, but what about a protected class. i could always subclass a protected class and use it then..

But the surrounding entity of the class would be the package - and you can't "subpackage" it...

So, i require the exact reason..

What do you require it for?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
ujsm
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't a class be declared private or protected