How we can changing the Implementation ?
Suppose the Car class requires to be used in a simulation of New York City traffic in that each actual car on the street is represented through one Car object. That's a lot of cars. As currently written every car object occupies around 60 bytes of memory (depending mostly on the size of the license plate string. We can knock off eight bytes per car through using floats instead of doubles, but the interface can stay the similar:
public class Car {
private String licensePlate; // e.g. "New York A456 324"
private float speed; // kilometers per hour
private float maxSpeed; // kilometers per hour
public Car(String licensePlate, double maxSpeed) {
this.licensePlate = licensePlate;
this.speed = 0.0F;
if (maxSpeed >= 0.0) {
this.maxSpeed = (float) maxSpeed;
}
else {
maxSpeed = 0.0F;
}
}
// getter (accessor) methods
public String getLicensePlate() {
return this.licensePlate;
}
public double getSpeed() {
return this.speed;
}
public double getMaxSpeed() {
return this.maxSpeed;
}
// setter method for the license plate property
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
// accelerate to maximum speed
// put the pedal to the metal
public void floorIt() {
this.speed = this.maxSpeed;
}
public void accelerate(double deltaV) {
this.speed = this.speed + (float) deltaV;
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
if (this.speed < 0.0) {
this.speed = 0.0F;
}
}
}
Since the interface is the similar, no other classes in which depend on this class requires to change or even be recompiled. We might save even more through using a custom LicensePlate class in which only permits one-byte ASCII characters instead of two byte Unicode characters.