In the previous lesson we covered packages and imports.
The Object class
You saw earlier that any class can extend (i.e., inherit from) any other class, and this can be another class written by you or someone else, or a Java supplied class.
In fact, every class always inherits from a special Java supplied class called Object, and this happens even if you don't specify it. Your Animal class declaration could be written as follows:
public abstract class Animal extends Object {
There is no need to specify extends Object, however. If you don't specify it, then it is assumed by Java. The class diagram can therefore be more accurately given as follows:
The above diagram shows that the Animal class directly extends from Object whereas Lion, Monkey and Penguin only extend it indirectly (since they directly extend from Animal). Objects of type Animal can do anything an Object can do, and objects of type Lion, Penguin and Monkey can do anything an object of type Animal can do, which includes what Object can do.
The Java API documentation tells you the inheritance hierarchy near the top of its page, for example the FileReader class page looks like this:
This shows that FileReader extends class InputStreamReader, which in turn extends class Reader, which in turn extends Object. The first three of these classes exist in the java.io package while Object exists within java.lang. (You will use the FileReader class in Section 19 – you don't need to understand it now).
The purpose of class Object is to provide certain basic functionality that is potentially useful for any class. The most important methods you need to know about that are defined within Object (and which therefore every class automatically inherits) are equals(), hashCode() and toString(). You will learn about the first two of these in a later section, but for now look at the API for Object and the description of its toString() method:
Since all classes inherit this method, you can invoke it already. Change the main() method in VirtualZoo to contain the following statements:
Animal leo = new Lion("Leo", "m", 4);
System.out.println(leo.toString());
The output will show something similar to the following:
run: virtualzoo.core.Lion@2e471e30 BUILD SUCCESSFUL (total time: 0 seconds)
The first part shows the qualified class name (i.e., prefixed by the package the class is within) followed by an @ symbol. After the @ symbol is a series of characters, and note that the characters you see will probably be different to those shown above, since they may, for example, relate to the memory location (expressed in hexadecimal[1]) where the object reference is located.
The primary use of toString() is to provide for each object a useful textual representation of that object. As it stands with the default implementation of this method inherited from Object, only the piece before the @ symbol is useful. Of more use would be for the method to list the values of its instance variables. Therefore, within the Animal class, override the toString() method as follows:
@Override
public String toString() {
return name + “, “ + gender + “, aged “ + age;
}
If you run VirtualZoo again you should see the following:
Leo, m, aged 4
Hopefully you will agree that this provides much more useful information than the default implementation defined in Object.
The toString() method is also implied when not stated, when used with the println() method, so you could write the code in VirtualZoo as:
Animal leo = new Lion("Leo", "m", 4);
System.out.println(leo);
There is no need to override the method again within classes Lion, Monkey or Penguin since they all inherit from the overridden one in Animal.
Because of the different types of animal it would be useful for toString() to return the actual type as well as the name, gender and age. Modify it as follows:
@Override
public String toString() {
return getClass().getSimpleName() + “ “ +
name + “, “ + gender + “, age “ + age;
}
- The
getClass()method is another method inherited fromObject, and which returns theClassobject associated with the class that the current object pertains to. - The
Classobject has invoked upon it agetSimpleName()method, which returns the textual name of the class (e.g., Lion, Monkey, etc, without its package prefix). - The above two methods are chained together. They could have been written as separate statements as follows:
Class c = getClass(); String className = c.getSimpleName();
When methods are chained together it saves the need to define temporary variable names. The chained methods are invoked from left to right, so firstly getClass() is executed to obtain a Class object, and then the getSimpleName() method is invoked upon this Class object. This returns a String object which becomes part of the text description.
The output should now be:
Lion Leo, m, aged 4
In the next lesson we will write a class to model a zookeeper.
Comments