The Object Class

The java.lang.Object class sits at the root of the Java class hierarchy and is therefore a superclass of every class you create. Object is unique in that it is the only class in Java without a superclass. The methods implemented by Object provide a framework for several general-purpose operations. You saw one of these methods, the toString() method, earlier in the StopLightColor class (refer to Listing 7.5). You can override toString() in the classes you declare to define a text representation of a class instance. Whenever your program has a need to display a value for an object, toString() is called to find out what to display. This is how a method such as System.out.println() can accept an Object as a parameter in place of ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.