This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
ok, encapsulation seems very important in good OOP. there first reason is clear, you can set boundaries checks to the value assigned to your instance variables along setter method the second reason is less clear. "you can change your mind without breaking everyone's code".
Let's have a non-computerised analogy. Let's imagine somebody asks me a question: "How far is it from here to Guisborough?"
Guisborough is a small town here in Yorkshire and is 10 miles from where I am sitting. So whenever you call my public double distanceTo(guisborough) method, it should return 10.0. 10.0 is the public return value and distanceTo(Town town) is the method. So Campbell has a part of his public interface which includes "distanceTo(Town town)". [In a lot of cases the return value is "don't know!"]
Behind the public interface however, I am encapsulating something, namely the workings of the "method." I am not telling you whether I measured it on the map with a piece of string, used an odometer, asked somebody else, or used one of those map websites which will happily send traffic along a trunk road or through a pedestrian precinct! Encapsulation means I can change the workings of the methods and the users are unaware of that fact. It means the users only use the method headers as shown in the javadoc, and any changes leave their code unaffected.
Another example: We don't know how the Math class works out the sin function. The formula which I found here is
sin(x) = x − x^3/3! + x^5/5! - x^7/7! + x^9/9!...
or more generally sin(x) = x^1/1! − x^3/3! + x^5/5! - x^7/7! + x^9/9!...
Now, you cannot tell whether the Math class has that formula in its method or whether it uses some other way to calculate sin. And it doesn't matter. What matters is that it gives the correct answer, not telling you that sin PI = 1, nor telling you that it is 18 miles to Guisborough.
BTW: Any mathematician will tell you sin PI is zero.
patrick van merchtn
Joined: Apr 04, 2007
I think its certainly up to me to say Well Done. You said it in other words then in the HF book and it became clear (except interface, still chapters to study in my HF book, thats now not a question).
Encapsulation means I can change the workings of the methods and the users are unaware of that fact. The users only use the method headers as shown in the javadoc, and any changes leave their code unaffected
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com