Java Reference
In-Depth Information
4.3.1
Class Object
Class Object , in package Java.lang , is automatically available in every Java
program. It enjoys a special status: every class that does not explicitly extend
another class, like Employee , automatically extends class Object . Object is the
superclass of all classes that do not extend other classes. Object is the “super-
est” class of them all.
Figure 4.8 shows some classes in a “tree”. The root of the tree is class
Object (computer scientists draw trees with their roots at the top). Attached to it
with lines underneath are two of its subclasses, Component and Employee ;
attached to them with lines underneath are their subclasses; and so on.
Class Object has a number of public instance methods, some of which are:
Activity
4-2.7
equals(Object) toString()
getClass() hashCode()
clone() wait()
These methods are inherited by every object, so there should be an Object par-
tition in every manila folder that we draw, as shown in Fig. 4.7. We usually do
not draw this partition because it would clutter the folders and because we know
it is in every folder.
You already know about function toString , whose purpose is to produce a
String representation of the folder in which it appears. It is a good practice to
override it in almost every class that you write.
Most of the methods in class Object are outside the scope of this text. But
one needs a full discussion: function equals .
4.3.2
Boolean function equals
Boolean function equals tests for the equality of the names of objects. Here is
its definition:
/** = " the name of this object is the same as the name of obj" */
public boolean equals(Object obj)
{ return this == obj; }
Function equals is often overridden to compare the contents of folders
instead of their names. Care must be taken when overriding it to ensure that it has
properties that are usually associated with equality. The specification of equals
in the Java API indicates this clearly: The overriding function should be an equiv-
alence relation . This means that it should have the following properties:
It is reflexive : for any folder x , x.equals(x) is true .
It is symmetric : for any folders x and y , x.equals(y) and y.equals(x)
have the same value.
It is transitive : for any folders x , y , and z , if x.equals(y) and
y.equals(z) are true , then x.equals(z) is true .
Search WWH ::




Custom Search