r/javahelp • u/migukau • May 09 '26
Codeless Question about oop encapsulation.
Let's say I have these classes:
House
HouseCatalgog- stores houses and other relevant information
SystemState- stores a HouseCatalog and other catalogs. Basically, there is an instance of this class that stores all the data my program uses and needs.
Menu - a menu class where the user can interact.
How should the menu class do something like change the name of a house from user input? Right now, it calls SystemState.changehousename(houseID, name), which then calls HouseCatalog.changehousename(houseID, name), which calls House.changename(name).
But I feel like this is C encapsulation and not correct for Java. My getters for the HouseCatalog class use a clone() so they don't return the actual pointer to houses I have stored.
Am I doing this wrong? Can I return the actual pointer from the house without breaking encapsulation, and then the Menu class just does House.changeName(name)?
View all comments
Show parent comment
2
u/AntD247 May 09 '26
This is certainly the direction I would also take as it leads to a more immutable data model as well as not making god objects.
Favour composition over inheritance.