«^»
4.8. Using the default version of toString

If you provide a class declaration but fail to provide a toString method, it is still possible for a program to apply the toString method to an object of the class. For example, if the toString declaration of Date's declaration is removed, the NoelProg program is still a valid program. When it is run, the program will execute the toString method of a class called Object. The two calls of println would then produce output that is something like:

Date@80cb419
Date@80cb419
This is the name of the class, followed by an @, followed by the hashcode of the object (given in the hexadecimal notation).

One of the key aspects of an object-oriented programming language such as Java is inheritance. This is a topic which will be described later. What we need to know at this stage is that a class is derived, by default, from a class called Object (belonging to the package java.lang). It is said to be a subclass of the class Object. This means that, if a program applies a method to an object, and the class of the object does not provide the method, but it is provided by the class Object, then Object's method will be called. The WWW page javaapi:java/lang/Object.html contains a list of the methods provided by the class Object: they are clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString and wait.

On the WWW page javaapi:java/lang/Object.html#toString(), it says: ‘ In general, the toString method returns a string that textually represents this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. ’ And this is exactly what is happening above: the definition of toString given in the class declaration for Date overrides the one given in Object.