Two Laptop Bag*
The moose likes Beginning Java and the fly likes encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "encapsulation" Watch "encapsulation" New topic
Author

encapsulation

patrick van merchtn
Greenhorn

Joined: Apr 04, 2007
Posts: 9
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".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
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?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Good job Campbell!




Regards,
cmbhatt


cmbhatt
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
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 − 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
Greenhorn

Joined: Apr 04, 2007
Posts: 9
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: encapsulation
 
Similar Threads
encapsulation in details
What is the difference between abstraction and encapsulation?
Encapsulation...
Field hiding
OO Concepts - Encapsulation, Cohesion, Coupling