Results 1 to 18 of 18
Thread: encapsulation in java
- 05-21-2011, 07:51 AM #1
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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?
- 05-21-2011, 07:57 AM #2
Cross posted 2 days ago and didn't reply to a helpful response.
encapsulation in java [Archive] - Ubuntu Forums
db
- 05-21-2011, 07:57 AM #3
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
Instead ofJava Code:public void getName() { return name; } public void setName(String nName) { if (nName.length() < 0) { name = nName; } }
EDIT: Saw DB's post. Will leave this one here, but shame on you OP.Java Code:object.name("My New Name");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.
- 05-21-2011, 07:54 PM #4
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!
- 05-21-2011, 08:01 PM #5
Not too many will agree on that one.Chiding the OP for posting the same question on the Ubuntu Forums is purely asinine, IMO.
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.
- 05-21-2011, 08:13 PM #6
-
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.
- 05-21-2011, 08:42 PM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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
- 05-22-2011, 05:59 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-23-2011, 11:00 AM #10
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
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...
- 05-23-2011, 11:08 AM #11
Member
- Join Date
- May 2011
- Posts
- 47
- Rep Power
- 0
- 05-23-2011, 11:16 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 05-23-2011, 11:52 AM #13
Member
- Join Date
- Apr 2011
- Location
- Athens, Greece
- Posts
- 52
- Rep Power
- 0
guest_user I think what are asking is having a get method to return the whole object something like 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 class myObject { int a; String b; public myObject getObject(){ return this; } }
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.Java Code:public static void main(String[] args){ MyObject obj = new MyObject(); someMethod(obj); }Last edited by santeron; 05-23-2011 at 11:58 AM.
- 05-23-2011, 12:03 PM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 05-23-2011, 12:06 PM #15
Member
- Join Date
- Apr 2011
- Location
- Athens, Greece
- Posts
- 52
- Rep Power
- 0
I'm getting a bit confused with this sentence and what exactly he needs...
Originally Posted by quest_user
- 05-23-2011, 12:39 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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):
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.Java Code:public class SomeClass { private Date someDate; public void getSomeDate() { return someDate; } }
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.
- 05-23-2011, 12:40 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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).
- 05-24-2011, 04:41 AM #18
Get in the habit of using standard Java naming conventions!
Similar Threads
-
A question about encapsulation
By 3awash in forum New To JavaReplies: 4Last Post: 04-20-2011, 12:16 PM -
Are abstraction and encapsulation concepts not in C?
By hitesh_public in forum New To JavaReplies: 1Last Post: 08-20-2010, 02:23 PM -
encapsulation & constructors
By buckskinner1776 in forum New To JavaReplies: 4Last Post: 05-10-2010, 06:35 PM -
java-Encapsulation
By lenah in forum Advanced JavaReplies: 5Last Post: 01-05-2009, 09:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks