Results 1 to 18 of 18
  1. #1
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default encapsulation in java

    1) if I have a private reference to an object and I provide a get method which returns it, does it break the encapsulation of the class since the client of the class can now obtain a reference to a private member object and change it?

    2) If the above answer is yes, I suppose the solution would be to clone the object when it is set and return another clone when it is get? Wouldn't that be very inefficient?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Cross posted 2 days ago and didn't reply to a helpful response.
    encapsulation in java [Archive] - Ubuntu Forums

    db

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    What do you mean by breaking the encapsulation? Encapsulation is making a direct reference to the variable impossible, and you use getters and setters. You aren't directly accessing the variable because you can now put checks in the code.

    ie
    Java Code:
    public void getName()
    {
         return name;
    }
    
    public void setName(String nName)
    {
          if (nName.length() < 0)
          {
                 name = nName;
          }
    }
    Instead of

    Java Code:
    object.name("My New Name");
    EDIT: Saw DB's post. Will leave this one here, but shame on you OP.
    Last edited by Dark; 05-21-2011 at 08:00 AM.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

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

    Default

    All right, that's enough. When did it become bad netiquette to ask the same question in entirely different user communities? Chiding the OP for posting the same question on the Ubuntu Forums is purely asinine, IMO.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Chiding the OP for posting the same question on the Ubuntu Forums is purely asinine, IMO.
    Not too many will agree on that one.
    Have you spent a lot of time answering an OPs question and then find it was already answered on another forum?
    Seems to me a big waste of time.
    Also by having links, anyone doing research on the topic can be the most answers when all the postings are linked together.

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

    Default

    Quote Originally Posted by Norm View Post
    Not too many will agree on that one.
    I'd be very surprised if that were so, considering that I've been using online forums since about 1993, and I've only ever encountered that attitude here... and in particular, only from db, who must spend sixteen hours a day policing these things.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kjkrum View Post
    I'd be very surprised if that were so, considering that I've been using online forums since about 1993, and I've only ever encountered that attitude here... and in particular, only from db, who must spend sixteen hours a day policing these things.
    I agree with Norm and Darryl, but the degree of my irritation depends on the amount of time and effort I've expended in trying to help the OP, and whether or not we've notified the poster of this issue before. The problem is not in cross-posting, which I believe is fine, but in cross-posting without notification. I've also seen similar policies on many programming forum sites including JavaRanch, the Oracle Java forums (and prior to that the Sun forums), and others.

    For instance in this thread: get the select row from a jtable

    The poster cross-posted here after receiving a correct answer at stackoverflow.com. So anyone answering the question here would likely just be doing busy work that helped no one and just wasted the helper's time. You could counter that and say that perhaps the OP didn't understand the answer, but I'd counter that still and state that if so, the OP should have requested clarification in the original forum, and if cross-posting at least notify all of the cross-post so that new helpers could at least see what has already been discussed. This isn't asking too much of posters and helps all involved.
    Last edited by Fubarable; 05-21-2011 at 08:32 PM.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    The example that fubarable posted is a perfect one - in which the question of the original poster was already answered. One could spend time trying to answer that the original poster may never read given their problem was solved elsewhere. I for one don't like wasting my time answering a post that has already been solved, when there are dozens of other posts that would better suit my time.
    The problems with cross-posting

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I agree with the general sentiment being expressed here. There is another poster on these forums who cross posts, however; he explicitly states on each forum he cross posts on that he posted somewhere else, he provides a link to the other forum post, and he responds to people on both forums. This, in my opinion is the way to so it. The op hasn't come back to this post once, and I doubt he planned to. I'm sure if we didn't provide a fully clarified answer he would have simply reposted to another forum. He probably would have repeated this until he found the full clarification. He could have saved our time, and his, by remaining in the original forums and asking for clarification.

    Darryl polices these forums so we don't have to deal with a lot of spammers and generally unwanted posts/behavior, and I believe most people here will be thankful.

    That being said; @op: you provide getters to allow the users to access the information, but it's still safely encapsulated, the getter gives them a view into the class, you don't even need to provide a getter to each variable, you can decide what to allow them to view.

    When it comes to setters, it does allow them to change the encapsulated variables, but in a way you control. If you are really interested in keeping the encapsulated data unmodifiable, you can make the class immutable. Doing this introduces more factors to consider to really limit the amount of change they can make on the class.

  10. #10
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Hi,
    I didn't realize that cross posting would cause so much hate, and I apologize for it although the reason I did so was to obtain answers from different people...

  11. #11
    guest_user is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post

    That being said; @op: you provide getters to allow the users to access the information, but it's still safely encapsulated, the getter gives them a view into the class, you don't even need to provide a getter to each variable, you can decide what to allow them to view.
    but if you provide getters and the get method returns a copy of the private reference to the object, the the client of the class would be able to invoke mutable methods on the object that the reference is pointing to would they not?

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't know if it's really hate, I think it's safer to say annoyance. People can be lazy, and generally speaking, it's not worth it to post an answer if you know the person has ignored previous answers on other forums. If you do cross post to obtain a more broad answer, I suggest you include a link to he other thread as well as the reasoning for cross posting.

    That being said, did my response help you understand?

  13. #13
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    guest_user I think what are asking is having a get method to return the whole object something like this?
    Java Code:
    public class myObject {
       int a;
       String b;
    
       public myObject getObject(){
          return this;
       }
    }
    If that's what you want then it's not a proper implementation. You should have already instantiated your object so you can use it's reference for anything you want it to.
    Java Code:
    public static void main(String[] args){
       MyObject obj = new MyObject();
       someMethod(obj);
    }
    You should clone the object only if you want to keep the original unchanged. If you just need the data from that object then there is no reason to clone it.
    Last edited by santeron; 05-23-2011 at 11:58 AM.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The op wanted clarification on encapsulation, if he doesn't want the class to be changed I suggest once again that he make the class immutable.

    I believe my first response did a decent job of explaining encapsulation and I'd be interested to hear from the op whether it helped him.

  15. #15
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by quest_user
    if I have a private reference to an object and I provide a get method which returns it
    I'm getting a bit confused with this sentence and what exactly he needs...

  16. #16
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    See the cross post referred to by Darryl.
    The reply explains it in there, but essentially (and this is a classic which FindBugs reports on):
    Java Code:
    public class SomeClass {
        private Date someDate;
        public void getSomeDate() {
            return someDate;
        }
    }
    My calling code can now use getSomeDate() and change the value of that Date object and the instance of SomeClass I got it from would be none the wiser.
    If that date object was used in the class to work with financial figures, say, then some important recalculation might be missed. The general solution is to wrap or clone.

  17. #17
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default

    Oh, and the anti cross posting thing was big on the old Sun forums (and now Oracle ones). So it's not just here (though some of us inhabit both).

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

    Default

    Quote Originally Posted by guest_user View Post
    but if you provide getters and the get method returns a copy of the private reference to the object, the the client of the class would be able to invoke mutable methods on the object that the reference is pointing to would they not?
    Correct.

    More words to make the spam filter happy.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. A question about encapsulation
    By 3awash in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 12:16 PM
  2. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 02:23 PM
  3. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 06:35 PM
  4. 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
  •