Java Reference
In-Depth Information
is a superclass of every class. You never need to specify the class
Object
as a base in the definition of your
classes — it happens automatically.
There are some interesting consequences of having
Object
as a universal superclass. For one thing, a
variable of type
Object
can store a reference to an object of any class type. This is useful when you want to
write a method that needs to handle objects of unknown type. You can define a parameter to the method of
type
Object
, in which case a reference to any type of object can be passed to the method. When necessary
you can include code in the method to figure out what kind of object it actually is (you see some of the tools
that enable you to do this a little later in this chapter).
Of course, your classes inherit members from the class
Object
. These all happen to be methods, of which
METHOD PURPOSE
toString()
This method returns a
String
object that describes the current object. In the inherited version of the
method, this is the name of the class, followed by
'@'
and the hexadecimal representation for the object.
This method is called automatically when you concatenate objects with
String
variables using
+
. You
can override this method in your classes to create your own string for an object of your class.
equals()
This compares the reference to the object passed as an argument with the reference to the current object
and returns
true
if they are equal. Thus
true
is returned if the current object and the argument are the
same object (not just equal — they must be one and the same object). It returns
false
if they are differ-
ent objects, even if the objects have identical values for their data members.
getClass()
This method returns an object of type
Class
that identifies the class of the current object. You learn a
little more about this later in this chapter.
hashCode()
This method calculates a hashcode value for an object and returns it as type
int
. Hashcode values are
used in classes defined in the package
java.util
for storing objects in hash tables. You see more about
this in Chapter 14.
notify()
This is used to wake up a thread associated with the current object. I discuss how threads work in
Chapter 16.
notifyAll()
This is used to wake up all threads associated with the current object. I also discuss this in Chapter 16.
wait()
This method causes a thread to wait for a change in the current object. I discuss this method in Chapter
16, too.
Note that
getClass()
,
notify()
,
notifyAll()
, and
wait()
cannot be overridden in your own class
definitions — they are
fixed
with the keyword
final
in the class definition for
Object
(see the section on
the
final
modifier later in this chapter).
It should be clear now why you could get polymorphic behavior with
toString()
in your derived classes
when your base class did not define the method. There is always a
toString()
method in all your classes
that is inherited from
Object
. This means that, ideally, we should have used the
@Overrid
e annotation with
this method in the
Animal
class, or indeed any class that implements
toString()
.
METHOD PURPOSE
clone()
This method creates an object that is a copy of the current object regardless of type. It can be of any type,
as an
Object
variable can refer to an object of any class. Note that
clone()
does not work with all class
objects and does not always do precisely what you want, as you see later in this section.
finalize()
This method may be called to clean up when an object is destroyed. There is rarely any reason to override
this method.