next up previous
Next: Composition (optional) Up: Unit 08 Previous: Class hierarchy

The class Object

All classes defined in Java are subclasses of the predefined class Object, even if this is not indicated explicitly.

This means that all classes inherit from Object several standard methods, such as equals(), clone(), and toString(), which are defined for Object. If we invoke one of these methods on some object belonging to a class C, but have not overridden it in C (or in one of its superclasses different from Object), the method defined in the class Object is used.

Now we can understand better that the possibility of ``redefining'' the toString() method discussed in Unit 3, actually corresponds to overriding the one of the class Object.

Note: The toString() method is automatically invoked by the print() and println() methods. This works correctly because of the mechanism of late-binding.


next up previous
Next: Composition (optional) Up: Unit 08 Previous: Class hierarchy