Page 5 of 9
Making attributes latitude and longitude private data members of class Position and adding simple accessor and mutator methods, also commonly called getters and setters, provides a simple remedy to exposing
raw data items. In the example code below, the setter methods appropriately screen the internal values of latitude and longitude. Rather than throw an exception, I specify performing modulo arithmetic on input values to keep the internal values within
specified ranges. For example, attempting to set the latitude to 181.0 results in an internal setting of -179.0 for latitude.
The following code adds getter and setter methods for accessing the private data members latitude and longitude:
public class Position
{
public Position( double latitude, double longitude )
{
setLatitude( latitude );
setLongitude( longitude );
}
public void setLatitude( double latitude )
{
// Ensure -90 <= latitude <= 90 using modulo arithmetic.
// Code not shown.
// Then set instance variable.
this.latitude = latitude;
}
public void setLongitude( double longitude )
{
// Ensure -180 < longitude <= 180 using modulo arithmetic.
// Code not shown.
// Then set instance variable.
this.longitude = longitude;
}
public double getLatitude()
{
return latitude;
}
public double getLongitude()
{
return longitude;
}
public double distance( Position position )
{
// Calculate and return the distance from this object to the specified
// position.
// Code not shown.
}
public double heading( Position position )
{
// Calculate and return the heading from this object to the specified
// position.
}
private double latitude;
private double longitude;
}
Using the above version of Position requires only minor changes. As a first change, since the above code specifies a constructor that takes two double arguments, the default constructor is no longer available. The following example uses the new constructor, as well as the
new getter methods. The output remains the same as in the first example.
Position myHouse = new Position( 36.538611, -121.797500 );
Position coffeeShop = new Position( 36.539722, -121.907222 );
double distance = myHouse.distance( coffeeShop );
double heading = myHouse.heading( coffeeShop );
System.out.println
( "From my house at (" +
myHouse.getLatitude() + ", " + myHouse.getLongitude() +
") to the coffee shop at (" +
coffeeShop.getLatitude() + ", " + coffeeShop.getLongitude() +
") is a distance of " + distance +
" at a heading of " + heading + " degrees."
);
Choosing to restrict the acceptable values of latitude and longitude through setter methods is strictly a design decision. Encapsulation does not play a role. That is, encapsulation, as manifested
in the Java language, does not guarantee protection of internal data. As a developer, you are free to expose the internals
of your class. Nevertheless, you should restrict access and modification of internal data items through the use of getter
and setter methods.
Protecting internal data is only one of many concerns driving design decisions on top of language encapsulation. Isolation to change is another. Modifying the internal structure of a class should not, if at all possible, affect client classes.
good read! thanks.By Anonymous on June 27, 2009, 3:47 amgood read! thanks.
Reply | Read entire comment
encapsulationBy Anonymous on June 25, 2009, 8:16 pmWell, according to the official Java tutorial from Sun your definition of encapsulation is not entirely true. Citing: "Hiding internal state and requiring all interaction...
Reply | Read entire comment
encapsulation presantionsBy Anonymous on March 16, 2009, 12:20 pmi m requered to give a presantion on this sub handing ENCAPSULATION to which i have no single idea where to start or where to look at.
Reply | Read entire comment
encapsulationBy Anonymous on February 16, 2009, 1:42 pmthe content u provided is quite satisfactory it helped me to clear my doubts in this topic
Reply | Read entire comment
the information I search in this web site help me so much im assignments,thanks!By Anonymous on November 5, 2008, 6:32 amthe information I search in this web site help me so much im assignments,thanks!
Reply | Read entire comment
View all comments