• Post Reply Bookmark Topic Watch Topic
  • New Topic

The notion of a "protected class"  RSS feed

 
Raghavendra Shockley
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it make sense to make all the members of a class protected ? If yes, then why go through the trouble of typing protected many times. Why not make the whole class protected, ie protected class MyClass ?
I know that java does not allow protected and private here. I understand why private makes no sense because that class will not be accessible at all and even its main() cant be run. Its the protected i cannot understand.

 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The access specifiers are only related to the identifiers whose access they specify. Making a class public doesn't automatically make all its members public. So if making a class protected has no bearing on its members, then what use is this specifier? Making a top level class protected simply does not make sense on a conceptual level. The same is true for private.

Having said that, you should avoid the protected keyword as much as possible. If you don't know exactly whether its use is correct, it probably isn't. Preferably, you should make most members private or package private.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!