In the previous lesson you learnt about overloading.
Inheritance
It was mentioned earlier that there is currently no obvious way of specifying what type of animal is being created. You might guess that Bruno could be a dog or that Tiddles could be a cat, but there is no guarantee of this.
For the zoo application the type of each animal is important to know, and a simplistic approach would be to introduce another instance variable within the Animal class to store its type (e.g., lion, monkey etc.). However, this approach is not very flexible; it is known, for example, that different animals have different feeding requirements, and trying to cater for all the different possibilities would become difficult to manage.
Object-oriented languages provide a feature known as inheritance whereby you can define a class as being "a kind of" a different class, and when you do this the inheriting class automatically adopts all of the capabilities of the class it inherits from.
In NetBeans, right-click on the virtualzoo.core package node and select New | Java Class..., and then enter Lion as the class name. After you click Finish the Java source should look as follows (ignoring comment lines):
package virtualzoo.core;
public class Lion {
}
To specify that a lion is a type of animal you need to make the Lion class inherit from the Animal class, which is done using the extends keyword:
package virtualzoo.core;
public class Lion extends Animal {
}
The class you inherit from (Animal in the above example) is known as the superclass and the one doing the inheriting (Lion in the above example) is known as the subclass.
The inheritance relationship between Animal and Lion can be shown diagrammatically by using a class diagram:
In a class diagram the subclass points toward the superclass to indicate that it inherits from it. The superclass does not have to be drawn above its subclasses, although it is common to do so. Class diagrams are an example of a range of diagramming techniques that form part of the Unified Modeling Language (UML).
Note that in Java a class can only extend one other class.
After entering the above, you will note a red exclamation point in NetBeans to the left of the class declaration line. If you hover your mouse over this, you will see an error message stating that no suitable constructor could be found. This is because constructors are not inherited, and therefore always need to be specified.
Include two constructors for the Lion class as follows (they follow the same pattern as the constructors within Animal:
public class Lion extends Animal {
public Lion(String myName, String myGender) {
this(myName, myGender, 0);
}
public Lion(String myName, String myGender, int myAge) {
super(myName, myGender, myAge);
}
}
Note the following:
- The names of the constructors must match the class name (
Lionin this case). - The constructor that requires only two arguments forwards to the constructor that requires three, in the same way as was defined in the
Animalclass. - The body for the constructor that requires three arguments does not assign to the instance variables in the same way as was the case within
Animal. Instead, thesuper()statement causes the constructor of the class from whichLionis inheriting from (Animalin this case) to be invoked, and it passes along the three supplied argument values. The constructor withinAnimalassigns the supplied values and then passes control back to theLionconstructor. - This process of a subclass constructor invoking a superclass constructor is in fact a requirement of Java, which enforces it in order to ensure that all state is properly initialised.
At this point, the Lion class is identical in terms of functionality to the Animal class. It automatically inherits all of its public methods without having to specify them: In VirtualZoo add the following import statement:
package virtualzoo;
import java.util.*;
import virtualzoo.core.Animal;
import virtualzoo.core.Lion;
public class VirtualZoo {
Now inside the main() method add the following:
Lion leo = new Lion(“Leo”, “m”, 8); System.out.println(leo.getName());
Note, however, that the instance variables defined within Animal are not directly available within Lion since they were declared to be private. Therefore, if within the Lion class you need access to these instance variables you need to invoke the appropriate methods that supply them, such as getName(), etc.
It is also possible to construct a Lion by specifying the reference type to be Animal:
Animal clarence = new Lion(“Clarence”, “m”, 12);
The above approach works because Lion is a type of Animal. As you become more familiar with Java you will see that declaring the reference type using the superclass can provide some advantages, such as when you need a group of different types of animals.
Now that you know how to create subclasses you will create two more to represent penguins and monkeys. In NetBeans, ensure the virtualzoo.core package node is expanded and right-click on the Lion node, and then select Copy.
Now, right-click on the virtualzoo.core package node and select Paste | Refactor Copy..., and in the Copy Class dialog enter Penguin in the New Name field, and finally click Refactor.
Double-click the Penguin.java node in the Projects window to open the source code. You will see that NetBeans has adjusted the class and constructor names for you:
package virtualzoo.core;
public class Penguin extends Animal {
public Penguin(String myName, String myGender) {
this(myName, myGender, 0);
}
public Penguin(String myName, String myGender, int myAge) {
super(myName, myGender, myAge);
}
}
Follow the same process to create another class called Monkey, which should look like this:
package virtualzoo.core;
public class Monkey extends Animal {
public Monkey(String myName, String myGender) {
this(myName, myGender, 0);
}
public Monkey(String myName, String myGender, int myAge) {
super(myName, myGender, myAge);
}
}
At this stage you can create animals in any of the following ways:
Animal animal1 = new Animal("Fred", "m", 2);
Lion animal2 = new Lion("Leo", "m", 8);
Penguin animal3 = new Penguin("Oswald", "m", 3);
Monkey animal4 = new Monkey("Bonzo", "f", 5);
You will need to import virtualzoo.core.Lion,virtualzoo.core.Penguin andvirtualzoo.core.Monkey.
They are all types of animals, and it is also therefore possible to specify the above as follows:
Animal animal1 = new Animal("Fred", "m", 2);
Animal animal2 = new Lion("Leo", "m", 8);
Animal animal3 = new Penguin("Oswald", "m", 3);
Animal animal4 = new Monkey("Bonzo", "f", 5);
In the next lesson you will learn about method overriding.
Comments