-
Normally, all object attributes are declared private.
-
This prevents objects from accessing other object attributes directly.
-
Such arrangement is dictated by the rules of encapsulation.
-
Accessor operations, known as setters and getters allow sharing appropriate information between objects.
|
-
For example,
public class Point {
int x;
int y;
public void setX( int x ) {
this.x = x;
}
public int getX() {
return x;
}
public void setY( int y ) {
this.y = y;
}
public int getY() {
return y;
}
}
-
Note: Encapsulation preserves data integrity.
|