Java Reference
In-Depth Information
class contain a
getName()
method. Therefore, the
Master
instance inherits the
getName()
method
directly from the
Person
class. Next, it will need to call the
calculateGPA()
method for John. The
Master
class does not contain a
calculateGPA()
method, but the
Graduate
class, the superclass of
Master
, does. Dynamic binding looks at the type of instance that John is: a
Master
and, therefore, a
Graduate
. Therefore, the
calculateGPA()
method from the
Graduate
class is called.
For Anne, the same decision process occurs. There is no
getName()
method in any of the subclasses,
so the class hierarchy is considered to find that Anne is an
Associate
, which is an
Undergraduate
,
which is a
Student
, which is a
Person
, and the
getName()
method from the
Person
class is called.
For her GPA, neither
Associate
nor
Undergraduate
has a
calculcateGPA()
method, so the
Student
version of the method is called for Anne.
Sure enough, the console output shows two different GPAs, despite having the same grades, because
dynamic method invocation allows different versions of the method to be called depending on the
instance calling it.
John Adams: 0.865
Anne Philips: 0.792
the superclass object
As mentioned in the introduction to inheritance, all classes in Java descend from the class
Object
.
The
Object
class has several methods that are, therefore, inherited by every other class. You may
choose to use them directly or override them in your own classes.
➤
protected Object clone()
: Creates and returns a copy of this object
➤
public boolean equals(Object obj)
: Indicates whether some other object is “equal to”
this one
➤
protected void finalize()
: Called by the garbage collector on an object when garbage
collection determines that there are no more references to the object
➤
public final Class getClass()
: Returns the runtime class of an object
➤
public int hashCode()
: Returns a hashcode value for the object
➤
public String toString()
: Returns a string representation of the object
The
clone()
method can be used only if the class is
Cloneable
. If so, the default
clone()
method
creates a new object of the same class as the original object and with the same values for its instance
variables. If your original object references another object, the clone will reference the same object.
You may want to override the
clone()
method to create clones of both objects instead of sharing
the referenced object.
The
equals()
method was discussed in Chapter 5. It compares two objects for equality. To compare
two objects, this method uses the
==
operator, which gives the correct answer for primitive data
types. However, for objects, it returns
true
if the references are equal, meaning the two variables
point to exactly the same object. In most cases, you are more interested if the instance variables' val-
ues of both objects match. To accomplish this, you should override the
equals()
method according
to the needs of your class or program. For example, you might consider two
Person
objects equal
if their social security number is the same. Depending on the class, there might be different possible
Search WWH ::
Custom Search