Methods
The simplest mechanism for encapsulating implementation details is a method.
The Math.sin
function that comes with Java has a definition that looks
something like this.1
double sin(double x) {
double[] y = new double[2];
double z = 0.0;
int n, ix;
// High word of x.
ix = __HI(x);
// |x| ~< pi/4
ix &= EXP_SIGNIF_BITS;
if (ix <= 0x3fe9_21fb) {
return __kernel_sin(x, z, 0);
} else if (ix >= EXP_BITS) { // sin(Inf or NaN) is NaN
return x - x;
} else { // argument reduction needed
n = RemPio2.__ieee754_rem_pio2(x, y);
// ...
}
}
People who write programs that depend on the sin
function do not generally
understand how this works. I have a math minor2 and I certainly don't.
All they need to know to use sin
effectively is what it does, not how it does it.
In this way methods are one way to provide encapsulation. You reduce
a potentially complicated body of code to inputs required and outputs produced.3
1
I took some creative liberties here, roll with it.
2
Laugh it up.
3
This shouldn't be news to you at this point, but I think its helpful to point out that property in the context of this topic.