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.
That any help?
Another example: We don't know how the Math class works out the sin function. The formula which I found here is
or more generally
sin(x) = x − x^3/3! + x^5/5! - x^7/7! + x^9/9!...
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.
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