Java Reference
In-Depth Information
public class Clock
{
//Declare instance variables as given in Chapter 8
//Definition of instance methods as given in Chapter 8
//...
}
is, in fact, equivalent to the following:
public class Clock extends Object
{
//Declare instance variables as given in Chapter 8
//Definition of instance methods as given in Chapter 8
//...
}
Using the mechanism of inheritance, every public member of the class Object can be
overridden and/or invoked by every object of any class type. Table 10-1 describes some
of the constructors and methods of the class Object .
TABLE 10-1 Constructors and Methods of the class Object
public Object()
//Constructor
public String toString()
//Method to return a string to describe the object
public boolean equals(Object obj)
//Method to determine if two objects are the same
//Returns true if the object invoking the method and the object
//specified by the parameter obj refer to the same memory space;
//otherwise it returns false.
protected Object clone()
//Method to return a reference to a copy of the object invoking
//this method
protected void finalize()
//The body of this method is invoked when the object goes out of scope.
Because every Java class is directly or indirectly derived from the class Object ,
it follows from Table 10-1 that the method toString becomes a public member
of every Java class. Therefore, if a class does not override this method, whenever this
method is invoked, the method's default definition executes. As indicated previously,
the default definition returns the class name followed by the hash code of the object
as a string. Usually, every Java class overrides the method toString . The class String
overrides it so that the string stored in the object is returned. The class Clock
 
Search WWH ::




Custom Search