|
In this example, we have a main class called PointMaker
, which makes and uses some objects of an instantiable class called Point
.
The Point
class does two things:
Point
object are.
Point
object are, and what they will do when they're called.
If we put these two classes into a CodeWarrior project and ran the program, the computer would go to the main method of the PointMaker
class and start executing the instructions there. Once it reached the end of the main method, it would stop.
So where does the Point
class fit in? The first thing to understand is that the Point
class is not executed line by line, the way the main class is. Each method in the Point
class gets executed only when it gets called. We'll go through the main class, line by line, and discuss how that happens. To get the most out of it, keep looking at the Point
class while you read this.
Point a = new Point(4,5);
This line declares an object a
of type Point
, and calls the Point
class constructor. It passes the constructor two integer parameters. When the computer sees this, it goes over to the Point
class to see if there's a definition for such a constructor. Since we wrote one, it does find one. Looking inside the method, it sees that it's supposed to put the value of the first parameter into the data member x
of the new object a
, and the value of the second parameter into the data member y
. Being a good computer, it does what it's told. So now (somewhere in the memory of the computer) there is a Point
object named a
, with data members x = 4
and y = 5
.
Point b = new Point(0,2);
The explanation for this line is about the same as the last one. Now, in addition to a
, there is another Point
object named b
, with data members x = 0
and y = 2
.
int xOfA = a.getX();
This line declares a local integer variable called xOfA
, and assigns a value to that variable. The value it assigns is the value of the expression a.getX()
. When the computer sees this expression, it checks to see if it has an object called a
somewhere (which it does). Then it checks to see if a
has a method called getX
with zero parameters. Since a
is a Point
object, that means it has to go over to the Point
class to see if there's a definition for such a method. It finds the one we wrote. Looking inside the method, it sees that it's supposed to return the value of a
's data member x
. So it returns 4
, and xOfA
gets the value 4
.
int xOfB = b.getX();
The explanation for this line is about the same as the last one. Now there is another local variable, called xOfB
, that has the value 0
.
Notice that we have two Point
objects in memory, a
and b
, but we still only have one Point
class, and therefore only one definition of the getY
method. How can the very same method return 4
in one call and 0
in another? The same way that I can ask two students what their names are, using the very same question, and get two different answers. The way I ask the question is the same, but since I'm asking two different students, the answers can be different. Likewise, the way the method works is the same, but since we call the method on the two different objects, the return values can be different.
a.invert();
This line calls a method of a
without doing any assignments. This makes sense, because the method invert
in the Point
class doesn't have any return value (that's what the word void
in its method header means). The computer goes to the Point
class to find the definition of this method, and follows the instructions inside. Those instructions say to switch the values of a
's data members x
and y
. So somewhere in memory, we have the same Point
object a
with its coordinates switched, so it represents (5,4) now. Note that this method actually changes the object on which it's called, while the method getX
does not. That's why this method is called a mutator, while getX
is called an accessor.
b.add(a);
This line calls b
's add
method and passes one parameter to it. The parameter that it passes is a reference to another Point
object, a
. The computer finds the definition for this method, and sees that it's supposed to add the parameter p
's data members to the corresponding data members in b
. What is p
? It's whatever point got passed to this method at the point of call. In this case, that means p
is a
. So the computer does the additions, and now b
represents (5,6). Note that a
does not get changed, because nothing is assigned to the data members of p
.
double dist = a.getDistanceFrom(b);
This line calls a
's getDistanceFrom
method and passes it a reference to b
. The computer finds the definition for this method, and sees that it's supposed to calculate the distance between the two points and return it. So the distance, which is 2
now because of the changes to a
and b
, gets assigned into the local double variable dist
.
Point invertOfB = b.getInvert();
This line calls b
's getInvert
method, and assigns the return value into the Point
variable invertOfB
. In order for this to work, the return type of getInvert
has to be Point
. This method returns a new Point
object with data members that are the same as the data members of b
, except switched. So now invertOfB
represents (6,5).
Point halfPoint = a.getHalfWayPoint(b);
This line calls a
's getHalfWayPoint
method and passes it a reference to b
, and then assigns the return value into the Point
variable halfPoint
. The method returns a new Point
object with data members that are halfway between those of a
and b
. So now halfPoint
represents (5,5).