• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Encapsulation - object/methods & Private instance variables  RSS feed

 
michael Hannigan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...

for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.

i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?

in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer... why would i pass something that doesn't make sense lol

anyway, i'm just a bit confused on this part....

The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?

If it's not too much trouble, I was wondering if anyone could clarify this for me or better yet, provide a better example of a simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?






 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
michael Hannigan wrote:in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer... why would i pass something that doesn't make sense lol

There are at least two reasons here.

1) Assuming you are only person who will work on the code you write will get you in trouble. When you start to program anything where other people are involved (be it professionally, open source, or just some fun with friends) then you will be sharing code with other people, and those people will be using the code you write. And if you let them use it wrong then the will use it wrong (because, if it is wrong you shouldn't let them use it that way. Since you let them do it, it must not be wrong). The other side of this is, whenever I come back to code I wrote 5 months or a few years ago, I always feel like it was written by someone else. So putting those sorts of restrictions in place protects your code from you, too.

2) There is an implied rule there - that the size of 0 is not a meaningful size. Maybe size 323545 isn't either. Anytime you have rules it is a good place to centralize those rules, so you don't have to duplicate code to check them, and you don't have worry about the rules not being checked. And the setSize() method is a great place to centralize the rules since it means you can't set the size without following the rules. But what if there were no rules? The setSize() method wouldn't hurt, in that case, but would allow you to add rules if, in the future, you determine there are rules - you could add rules on what size is allowed and not worry about finding all the code which sets the size and making sure they follow them, you add the rules into setSize() and all the callers get the rules-check automatically.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, you don't necessarily have setSize methods. You can replace the setSize method with a grow method which increases the Dog's size by 1. That still permits people to write this sort of thing however :A grow method may reflect real‑life dogs better than setSize.
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to expand on what's been said:

You want your methods and classes to be "black boxes" as much as possible. That means they should have defined input, behavior, and output. It should be very clear what they rely on, if anything.

For a trivial example, put in a first name and a last name, "push a button" (call the method, create the object) and out comes "lastname, firstname". How you do this should be hidden, and it shouldn't matter. The input should be validated; the output, documented.

Think of Legos or TinkerToys. You want everything to fit together as easily as possible. Think of the person who uses your method/class. Think of the person who has to modify your method/class.

And keep questioning things! It's good to know why something is done a certain way rather than just accepting it.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
i understand your point, the book you study is great, but one month ago i was stuck on the same problem, as does not make clear what is the real purpose of.

I am understanding encapsulation now that i am starting to think object oriented.

Imagine, what would happen if you do the next google car and the user put how fast the car have to go with a setter... it would not be really safe and cool having a class with a setter with a private variable, and a condition inside that he will use the instructions given by the user only if the car goes less than 180 km/h???

I assume if you google "getters and setters java" or " encapsulation java" or you look these keywords on the tube after few tutorials will be real clear in your mind.
enjoy your coding time.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually what you might do well to read about is the “Bean Pattern”. This is a specification for objects which can be written into an XML file and then re‑created from that same file years later. It is usually necessary to provide public set and get methods, as well as a no‑arguments constructor, for that pattern to work. Once you see how that works, you understand the necessity for set methods in some classes.

Also see if you can find a copy of Effective Java™ by Joshua Bloch and find the section about immutable objects or minimising mutability. That will give you a different view of the whole question.
 
michael Hannigan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:Michael,
i understand your point, the book you study is great, but one month ago i was stuck on the same problem, as does not make clear what is the real purpose of.

I am understanding encapsulation now that i am starting to think object oriented.

Imagine, what would happen if you do the next google car and the user put how fast the car have to go with a setter... it would not be really safe and cool having a class with a setter with a private variable, and a condition inside that he will use the instructions given by the user only if the car goes less than 180 km/h???

I assume if you google "getters and setters java" or " encapsulation java" or you look these keywords on the tube after few tutorials will be real clear in your mind.
enjoy your coding time.


ya, i got Head First Java, Thinking In Java, and Dietel's Java book for my learning; decided to start with Head First.
I must say, this book is incredible at explaining things in a way that's simple to understand.


by the way, i'm new to this forum, but i must say, this is such a fantastic community full of people who are eager to help one another!
you guys are awesome !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!