Results 1 to 6 of 6
  1. #1
    chinthanabt is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Java Encapsulation

    Hi,i am new to java.

    so i want to know about the encapsulation in OOP.

    As a example ,we creating private varibles in our classes but getters and setters are public why is that,

    and why we want to encapsulate particular module? please help me thanks all.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,958
    Rep Power
    8

    Default Re: Java Encapsulation

    Encapsulation is a little fuzzy, but it can maybe be summarized with a few examples.

    Imagine you're on a programming team, and you want to create a pong game. It's one person's job to program the keyboard listener, it's another person's job to program the ball, and it's another person's job to program the paddles.

    They person programming the keyboard listener doesn't care about the internals of how the paddles work. All he cares about is the goUp() and the goDown() methods. The keyboard programmer doesn't have to directly touch the y position of the paddles; he lets the paddles handle that themselves.

    Similarly, the person programming the paddles doesn't care about the internals of the ball. Let's say it's the ball programmer's job to check for collisions. Whenever the ball moves, the code (in the Ball class) checks whether it collided with a paddle or a side of the screen. The Ball class might even have a public getter and setter for the position- that way whenever the ball's position is set, it can check itself for collisions. But if the ball's position was public and a new programmer accidentally set the position without checking for collisions, the ball code would be completely broken, but without the ball programmer's knowledge.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Encapsulation

    w.r.t. those getters and setters; what you often see in students code is this:

    Java Code:
    private T property;
    
    private void setProperty(T property) { this.property= property; }
    private T getProperty() { return property; }
    The getter and setter in this code are completely useless here; the property itself could've been 'public'; at least the setter method should do something like testing for a sensible value and possibly correcting it if needed. Also compare this with the excellent answer given in the previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  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: Java Encapsulation

    Jos's example is not quite completely useless. Even though a getter and setter like that do nothing now, you should still write them that way so that you can make them do something later with less of a break to the API. If the behavior of a method changes and arguments that worked before no longer work, you've still broken the API. But IMO, this is less problematic than a change that breaks compilation. More esoterically, some bytecode enhancement frameworks (like database persistence helpers) depend on the presence of getters and setters.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Java Encapsulation

    It seems you are saying they are useless because they don't enforce invariants. At first glance, I thought you meant because the setters and getters are declared private.

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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java Encapsulation

    Quote Originally Posted by jim829 View Post
    It seems you are saying they are useless because they don't enforce invariants. At first glance, I thought you meant because the setters and getters are declared private.
    That 'private' thing was a copy and paste mistake of mine, sorry about that. As kjkrum already pointed out: there is something 'instable' about that code (empty getters and setters): it is about to be changed without attempting to break the API; as the code is now (and if it doesn't need to be changed), the property might have been declared public and those empty setters/getter could be removed from the API.

    kind regards, (and happy New Year!)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Encapsulation
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 6
    Last Post: 08-17-2013, 05:31 AM
  2. polymorphism,encapsulation....?
    By apple1992 in forum New To Java
    Replies: 4
    Last Post: 03-05-2012, 01:35 AM
  3. encapsulation in java
    By guest_user in forum New To Java
    Replies: 17
    Last Post: 05-24-2011, 04:41 AM
  4. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 06:35 PM
  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
  •