In the previous lesson you learnt about using classes and methods.
Method overloading
The Animal class currently defines a constructor which requires three argument values to be supplied (for the name, gender and age), and if when you invoke this constructor you supply the wrong number or the wrong type, the compiler will give you an error message.
You may consider that it would be useful for client objects to create newly born animal objects (i.e., whose age would be zero) without having to specify the third argument value, purely as a matter of convenience. Add the following constructor definition above the existing constructor in the Animal class:
public Animal(String myName, String myGender) {
this(myName, myGender, 0);
}
public Animal(String myName, String myGender, int myAge) {
name = myName;
gender = myGender;
age = myAge;
}
Note the following:
- This new constructor only requires two
Stringarguments to be supplied - The
this()statement inside the constructor body passes the two supplied strings along with theintvalue zero to the constructor that requires all three arguments to be specified. The zero effectively becomes a default value for the age. The process of one constructor forwarding to another is a useful technique that prevents you from having to duplicate code. Without it, you would have had to have coded the body as follows:
public Animal(String myName, String myGender) {
// DON'T DO THIS – USE THE TECHNIQUE ABOVE IN PREFERENCE
name = myName;
gender = myGender;
age = 0;
}
Defining more than one constructor within a class is known as constructor overloading and is a common technique that allows client objects more flexibility in how they instantiate objects of your class. For example, to create a new-born animal you can now use either of the following two approaches, whichever is most convenient:
Animal aCat1 = new Animal("Tiddles", "f"); // age of zero assumed
Animal aCat2 = new Animal("Arthur", "m", 0); // age of zero specified
It is also possible to overload methods. For example, some animals may have a nickname, but instead of defining a separate instance variable for these odd cases you could instead overload the setName() method so that in addition to the current one which requires one String argument you define a second one which requires two:
public void setName(String newName) {
name = newName;
}
public void setName(String newName, String nickName) {
setName(newName + "(also known as " + nickName + ")");
}
Note the following:
- The second version of
setName()requires twoStringarguments, and when supplied in this way will append the nickname to the proper name which is still supplied in the first argument value. - The method body forwards to the
setName()method which requires only one argument.
Try the following statements in the VirtualZoo class:
bruno.setName("Bruno", "The Beast");
System.out.println("Name: " + bruno.getName());
The output from the above should show:
Name: Bruno(also known as The Beast)
In the next lesson you will learn about inheritance.
Comments