In the previous lesson we looked at abstract classes and methods.
Tidying up the Animal constructors
Since making the Animal class abstract the only constructor that ever gets invoked within Animal is the one requiring three arguments, which is called by its subclasses using the super() statement. The two-argument constructors in these subclasses forward to the three-argument one in the same subclass – hence the two-argument constructor of Animal is now redundant.
Although not essential, to remove it would be good discipline. This enforces the idea to subclasses that they need to supply all three arguments. Therefore, the only remaining constructor that should remain within Animal is the following:
// Define constructor
public Animal(String myName, String myGender, int myAge) {
name = myName;
gender = myGender;
age = myAge;
}
To round off this section you will see the updated class diagram that takes account of the fact that there are now three subclasses of Animal, and that Animal is now an abstract class:
You will notice that Animal is shown in italics to indicate that it is an abstract class. Non-abstract classes are commonly known as concrete classes and are indicated in normal upright text.
In the next section of lessons you will learn about packages, the Object class, and interfaces.
Comments