Results 1 to 4 of 4
Thread: Question about encapsulation
- 02-18-2013, 07:25 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 15
- Rep Power
- 0
Question about encapsulation
Hi,
I have a question about encapsulation and writing getter methods. I'm writing a class that has private field which is an object. I know that sometimes getters and setters are used to let others access those methods indirectly. So i write a getter that returns the private field object. So I'm actually returning a reference to the object. Can someone then use that reference to change fields inside of the object? Does this break encapsulation since while the field is private, after using the getter method someone can actually change the fields of the private object?
In the case of a private field that is a primitive, this is not an issue right?
Thank you.
- 02-18-2013, 09:28 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Question about encapsulation
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-18-2013, 12:06 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Question about encapsulation
Or use a wrapper, if the design is such that external code should not have access.
Easier to do if you use interfaces, so both the editable class and the wrapper can use the same interface.
eg
Java Code:public interface Example { int getValue(); } ... public class EditableExample implements Example { private int value; public int getValue() {return value;} public void setValue(int v) {value = v}; } ... public class ImmutableExampleWrapper implements Example { private Example ex; public ImmutableExampleWrapper (Example ex) { this.ex = ex; } public int getValue() {ex.getValue()}; }Please do not ask for code as refusal often offends.
- 02-18-2013, 06:25 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 902
- Rep Power
- 1
Re: Question about encapsulation
You should also be aware that even primitive arrays need to be defensively copied since arrays are passed by reference. An alien method could alter the contents of the array. And one other caution is that inheritance breaks encapsulation. So unless you are familiar with the internals of the super class or you need to provide access to only a few of the super class methods, you may want to employ composition. This is where you have a local reference of the super class and then implement forwarding methods within the subclass. It is similar to Tolls wrapper class except that the instance is already there, put in place by the sub class.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Can someone post an example where encapsulation is useful?
By Derangedyeti in forum New To JavaReplies: 4Last Post: 04-13-2013, 07:38 PM -
polymorphism,encapsulation....?
By apple1992 in forum New To JavaReplies: 4Last Post: 03-05-2012, 01:35 AM -
Confused about encapsulation
By 007 in forum New To JavaReplies: 9Last Post: 06-23-2011, 11:33 AM -
A question about encapsulation
By 3awash in forum New To JavaReplies: 4Last Post: 04-20-2011, 12:16 PM -
encapsulation & constructors
By buckskinner1776 in forum New To JavaReplies: 4Last Post: 05-10-2010, 06:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks