Most read:
Popular archives:
JavaWorld's new look is here!
We've upgraded the site with a fresh look-and-feel, improved topical navigation, better search, new features, and expanded
community platform. Learn more about the changes to JavaWorld.
| Oracle Compatibility Developer's Guide |
| The Explosion in DBMS Choice |
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.