Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't a class be declared private or protected

 
saloni pai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to declare a class as private or protected? How would you access it?
 
saloni pai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic