Why Is Java Missing Access Specifiers?

Posted by Tom Tresansky on Stack Overflow See other posts from Stack Overflow or by Tom Tresansky
Published on 2010-06-14T17:21:29Z Indexed on 2010/06/14 17:32 UTC
Read the original article Hit count: 155

Does anyone understand why Java is missing:

  • An access specifier which allows access by the class and all subclasses, but NOT by other classes in the same package? (Protected-minus)
  • An access specifier which allows access by the class, all classes in the same package, AND all classes in any sub-package? (Default-plus)
  • An access specifier which adds classes in sub-packages to the entities currently allowed access by protected? (Protected-plus)

I wish I had more choices than protected and default. In particular, I'm interested in the Protected-plus option.

Say I want to use a Builder/Factory patterned class to produce an object with many links to other objects. The constructors on the objects are all default, because I want to force you to use the factory class to produce instances, in order to make sure the linking is done correctly. I want to group the factories in a sub-package to keep them all together and distinct from the objects they are instantiating---this just seems like a cleaner package structure to me.

No can do, currently. I have to put the builders in the same package as the objects they are constructing, in order to gain the access to defaults. But separating project.area.objects from project.area.objects.builders would be so nice.

So why is Java lacking these options? And, is there anyway to fake it?

© Stack Overflow or respective owner

Related posts about java

Related posts about object-oriented-design