'Friends' equivalent for Java?

Alex Lim picture Alex Lim · Dec 18, 2008 · Viewed 55.5k times · Source

having a little architectural trouble here.

In C++, we have the notion of 'friends,' where such friend classes can access private members.

So, I'm deving a Java app and trying to adhere to the MVC architecture. I've got a controller class that manages graph connectivity between 'map_objects.' I'd like to hide the function in the DTO 'map_objects' that actuall sets up these connectivities, by using this controller class.

(Ie, even if the controller class implements the required functionality of setting up connectivities, the 'users' can still access setter/getter functions in the the DTO directly to set them up themselves.)

Are there any design patterns or tips in this regard? (Or have I totally mucked up?)

DUPLICATE Is there a way to simulate the C++ 'friend' concept in Java?

Answer

Julien Chastang picture Julien Chastang · Dec 18, 2008

(Un)fortunately, there is no direct C++ friend equivalent in Java. However, the Java access level modifiers can assist you. In particular, private or package private (AKA package protected, or "default") may help.