Results 1 to 7 of 7

Thread: Encapsulation

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Encapsulation

    From the SCJP book:

    Java Code:
    public class BadOO {
    public int size;
    public int weight;
    ...
    }
    public class ExploitBadOO {
    public static void main (String [] args) {
    BadOO b = new BadOO();
    b.size = -5; // Legal but bad!!
    }
    }
    Quoting the SCJP book author below:

    "If you want maintainability, flexibility, and extensibility (and of course, you do),
    your design must include encapsulation. How do you do that?
    ■ Keep instance variables protected (with an access modifier, often private).
    ■ Make public accessor methods, and force calling code to use those methods
    rather than directly accessing the instance variable.
    ■ For the methods, use the JavaBeans naming convention of
    set<someProperty> and get<someProperty>."


    The author is telling instead of having direct access to a variable of the class we can have accessor methods. So when I set a value for a variable using a setter method it is still the same as directly accessing the variable. Where is the benefit?

    Partially modifying the code snippet above I can say b.setSize(-5) instead of b.size=-5. So where is the advantage?
    Last edited by suhaas.mohandos@gmail.com; 08-16-2013 at 05:38 AM.

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Encapsulation

    Setter methods are to apply some restrictions on input or to take modified input say, what If you want size always positive ... you can do it in setter method ... restrict the size to positive values ....

    Java Code:
    void setSize(int size){
    if(size < 0)
    size *= -1;
    this.size = size;
    }
    Last edited by allaudin; 08-16-2013 at 06:44 AM.

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

    Default Re: Encapsulation

    The benefit is two-fold. First, it allows validity checks to be applied to the values. Second, it hides the nature of the field which permits changing the internals without affecting existing applications. Say you have the following two methods:

    Java Code:
    int value;
    public void setInteger(int a) {
     this.value = value;
    }
    public int getInteger() {
       return this.value;
    }
    // Now sometime in the future, I can do this (if I wanted to)
    
    private int[] values = new int[10];
    
    public void setInteger(int a) {
    values[0] = a;
    }
    
    public int getInteger() {
      return values[0];
    }
    The user is totally unaware that the internals were changed and the class may still be used as before.

    Additionally, I can also provide a wrapper class to return an immutable instance of the original class by overridding the setMethods to throw an IllegalOperationException or similar message.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Encapsulation

    The user is totally unaware that the internals were changed and the class may still be used as before.

    Is this the concept of abstraction?

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

    Default Re: Encapsulation

    No, encapsulation. The details are hidden from the client. At least, that's the way I see it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: Encapsulation

    But abstraction is about hidding the inner details, isn't it?

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Encapsulation

    Abstraction is the what, encapsulation is (part of) the how.
    Get in the habit of using standard Java naming conventions!

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, 07:38 PM
  2. Question about encapsulation
    By kprime in forum New To Java
    Replies: 3
    Last Post: 02-18-2013, 06:25 PM
  3. polymorphism,encapsulation....?
    By apple1992 in forum New To Java
    Replies: 4
    Last Post: 03-05-2012, 01:35 AM
  4. Confused about encapsulation
    By 007 in forum New To Java
    Replies: 9
    Last Post: 06-23-2011, 11:33 AM
  5. java-Encapsulation
    By lenah in forum Advanced Java
    Replies: 5
    Last Post: 01-05-2009, 09:05 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
  •