Objects First |
Compare the Java:
public double XCoord() {
return x;
}
In Java, this is called with:
Point p; p = new Point( a, b ); x = p.XCoord();Note how the object on which the method operates precedes the name of the method. Java, with formal support for OO design, provides a new keyword which precedes the constructor. The name of the constructor is also the same as the name of the class. (This is a requirement in Java - as distinct from the convention that we adopted in C.) Within the implementation of the method, attributes, such as x in this example, can be referenced directly. Java "understands" the connection between an object and its attributes, so there isn't the need to put them all into a struct as we do in C. |
with the C:
double XCoord( Point p ) {
return p->x;
}
In C, we provided the point on which the method operates as
the argument of the function:
Point p;
p = ConsPoint( a, b );
x = XCoord( p );
In C, without formal support for the OO design
methodology, you must specify the object on which the
method operates as one of the function parameters.
|
A more elaborate example in Java: the Histogram class.
Example of a Java program that uses the Histogram class.
We're indebted to Sofie Hon for these examples!
| Back to the Table of Contents |