Results 1 to 4 of 4
  1. #1
    kprime is offline Member
    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,731
    Blog Entries
    7
    Rep Power
    21

    Default Re: Question about encapsulation

    Quote Originally Posted by kprime View Post
    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.
    Yep, that's true and true; if you don' want that (non-primitive) fields to be changed by others, either don't suppy a getter method at all or make your getter method return a copy of your field.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,180
    Rep Power
    20

    Default 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.

    ** This space for rent **

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,920
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Can someone post an example where encapsulation is useful?
    By Derangedyeti in forum New To Java
    Replies: 4
    Last Post: 04-13-2013, 08:38 PM
  2. polymorphism,encapsulation....?
    By apple1992 in forum New To Java
    Replies: 4
    Last Post: 03-05-2012, 02:35 AM
  3. Confused about encapsulation
    By 007 in forum New To Java
    Replies: 9
    Last Post: 06-23-2011, 12:33 PM
  4. A question about encapsulation
    By 3awash in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 01:16 PM
  5. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 07:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •