Java Reference
In-Depth Information
How It Works
The
PolyLine
class implements all the methods that we had in the class before, so the
main()
method in the
TryPolyLine
class works just the same. Under the covers, the methods in the
PolyLine
class work a little differently. The work of creating the linked list is now in the constructor
for the
LinkedList
class. All the
PolyLine
class constructors do is assemble a point array if
necessary, and call the
LinkedList
constructor. Similarly, the
addPoint()
method creates a
Point
object from the coordinate pair it receives, and passes it to the
addItem()
method for the
LinkedList
object,
polyline
.
Note that the cast from
Point
to
Object
when the
addItem()
method is called is automatic. A cast
from any class type to type
Object
is always automatic because the class is up the class hierarchy -
remember that all classes have
Object
as a base. In the
toString()
method, we must insert an
explicit cast to store the object returned by the
getFirst()
or the
getNext()
method. This cast is
down the hierarchy so you must specify the cast explicitly.
You could use a variable of type
Object
to store the objects returned from
getFirst()
and
getNext()
, but this would not be a good idea. You would not need to insert the explicit cast, but you
would lose a valuable check on the integrity of the program. You put objects of type
Point
into the list,
so you would expect objects of type
Point
to be returned. An error in the program somewhere could
result in an object of another type being inserted. If the object is not of type
Point
- due to the said
program error for example - the cast to type
Point
will fail and you will get an exception. A variable
of type
Object
can store anything. If you use this, and something other than a
Point
object is
returned, it would not register at all.
Now that we have gone to the trouble of writing our own general linked list class, you may be
wondering why someone hasn't done it already. Well, they have! The
java.util
package defines a
LinkedList
class that is much better than ours. Still, putting our own together was good experience,
and I hope you found it educational, if not interesting. We will look at the
LinkedList
class in the
java.util
in Chapter 12.
Using the final Modifier
We have already used the keyword
final
to fix the value of a static data member of a class. You can
also apply this keyword to the definition of a method, and to the definition of a class.
It may be that you want to prevent a subclass from overriding a method in your class. When this is the
case, simply declare that method as
final
. Any attempt to override a
final
method in a subclass will
result in the compiler flagging the new method as an error. For example, you could declare the method
addPoint()
as
final
within the class,
PolyLine
, by writing its definition in the class as:
public final void addPoint(Point point) {
ListPoint newEnd = new ListPoint(point); // Create a new ListPoint
end.setNext(newEnd); // Set next variable for old end as new end
end = newEnd; // Store new point as end
}
Any class derived from
PolyLine
would not be able to redefine this method. Obviously an
abstract
method cannot be declared as
final
- as it must be defined in a subclass.