Make java methods visible to only specific classes

无人久伴 提交于 2019-11-28 12:40:30

Why not have an interface called

ManagerFunctions

and another called

ClientFunctions

You managed objects will implement both of these.

When you create the managed objects, you pass them around, but only as references to ClientFunctions. The manager objects will, however, refer to them as ManagerFunctions and consequently have access to their 'managed' functions. The appropriate casting will simply expose the appropriate methods.

Your IDE will automatically present you wil the appropriate methods depending on how these objects are referenced.

You're asking for something akin to the "friend" declarations of C++, but there's no direct equivalent in Java - package visibility is the nearest. Alternatively you could go for a model like the XML DOM, where the methods that should be public are defined in interfaces and all client access is via these interfaces. The manager would know the concrete class of the implementation so could downcast to that as required.

As such I'd like to have the managers have access to manipulating methods, but restrict access to every other class. The most obvious one would be to move the manager class and object class into the same package and declare manipulating methods protected...

Technically, you would declare the manipulating methods package protected (no modifier at all). Protected methods allow the class to be extended easier.

but as the managers and objects are completly seperate entities they don't fit there philosophically.

I understand. Java doesn't have the "friend" declaration that C++ has.

You could comment the manipulating methods, but that doesn't solve your Eclipse problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!