# encapsulation

patrick van merchtn

Greenhorn

Posts: 9

Campbell Ritchie

Sheriff

Posts: 50175

79

posted 9 years ago

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.

That any help?

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?

Campbell Ritchie

Sheriff

Posts: 50175

79

posted 9 years ago

Glad to help, Chandra Bhatt.

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^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.

Another example: We don't know how the Math class works out the sin function. The formula which I found here is

or more generallysin(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.

patrick van merchtn

Greenhorn

Posts: 9

posted 9 years ago

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