Results 1 to 5 of 5
  1. #1
    Derangedyeti is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Can someone post an example where encapsulation is useful?

    I have been trying to figure out how/why I should use encapsulation, but for some reason it just isn't clicking. Can anyone clarify? Nothing in the book I am reading at the moment is making it clear.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Can someone post an example where encapsulation is useful?

    From what I gathered, it is supposed to make it so that a particular class has complete control over what is stored inside it. When you write a class, a person who is using it doesn't need to know how it's implemented. That is, how it actually does what it is supposed to do. Encapsulation prevents them from messing with anything inside the class, be it intentional or accidental.

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

    Default Re: Can someone post an example where encapsulation is useful?

    This is how I think of it:

    Consider a radio. It provides a capability to do something with public controls. However, you don't need access to the resistors, transistors or other discrete components to use the radio. And the original makers can improve the radio by using more efficient and perhaps less expensive components. But it won't affect how you use the radio. But it may cost less and get better reception. The same is true for oo encapsulation. It only exposes those methods and fields necessary to provide a specific function. And later on someone can perhaps improve on those methods without affecting the way it is used by others.

    Java as well as other oo languages support extending classes and overriding existing methods which can break encapsulation. So this should be done carefully and in accordance with the extended classes documentation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Can someone post an example where encapsulation is useful?

    It's not something you can easily see an example of, because it's something that becomes more important as the size and complexity of a program grows.

    But I can tell you about an example. My job currently involves maintaining and trying to add features to a program that some jackass contractor wrote with basically zero encapsulation. It has tons of global state, and worse, that state is duplicated in several places. It's a huge pain in the ass to figure out when and where a variable was assigned the value that it has, or what effect assigning a different value might have on some distant part of the program. In a properly encapsulated program, all the causes and effects are right there in the class or method you're looking at.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Can someone post an example where encapsulation is useful?

    Here's an extremely contrived example. Consider two versions of the same class:

    Java Code:
    public class Foo {
        public int a;
        public int b; // should always be a * 2
    }

    Java Code:
    public class Foo {
        private int a;
        private int b; // should always be a * 2
    
        public void setA(int a) {
            this.a = a;
            this.b = a * 2;
        }
    
        public int getA() {
            return a;
        }
    
        public int getB() {
            return b;
        }
    }
    With the first version, every user of the class has to know that b is supposed to be a * 2. Inevitably something is going to muck it up, and then some other part of the program that relies on b being a * 2 is going to break. With the second version, the class enforces its own rules. Not only do the users of the class not have to worry about the rules, they may not even know what the rules are. This prevents them from depending on the rules, so they won't break if the rules change.

    In the real world, the rules that get encapsulated are often things like, "always close socket A and interrupt thread B before closing file C". Expose that kind of rule, and something is inevitably going to screw it up. Encapsulate it, and external code can't screw it up.
    Last edited by kjkrum; 04-13-2013 at 08:44 PM.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Confused about encapsulation
    By 007 in forum New To Java
    Replies: 9
    Last Post: 06-23-2011, 12:33 PM
  2. A question about encapsulation
    By 3awash in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 01:16 PM
  3. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 07:35 PM
  4. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  5. java-Encapsulation
    By lenah in forum Advanced Java
    Replies: 5
    Last Post: 01-05-2009, 10: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
  •