Exercise 2: Working With Interfaces and Abstract Classes (Level 2)

In this exercise, you will  create abstract classes and interfaces, and explore the polymorphic properties of these types of components. You will create a hierarchy of animals that is rooted in an abstract class Animal. Several of the animal classes will implement an interface called Pet. Use Figure 7-2 on page Lab 7-9 as a reference. You will experiment with variations of these animals, their methods, and polymorphism.

Task 1 - Creating the Animal Classes and Pet Interface

Create a source file for each class and interface shown in Figure 7-2 on page Lab 7-9. Make the action methods (walk, eat and play) print a statement to standard output that reflects the animal. For example, the walk method for the Animal class might say something similar to This animal walks on 4 legs, where 4 is the value of the legs attribute.

1. Create the Animal class, which is the abstract superclass of all animals.
a. Declare a protected integer attribute called legs, which records the number of legs for this animal.
b. Define a protected constructor that initializes the legs attribute.
c. Declare an abstract method eat.
d. Declare a concrete method walk that prints out something about how the animals walks (include the number of legs).

2. Create the Spider class.
a. The Spider class extends the Animal class.
b. Define a no-arg constructor that calls the superclass constructor to specify that all spiders have eight legs.
c. Implement the eat method.

3. Create the Pet interface specified by the UML diagram.

4. Create the Cat class.
a. The Cat class extends the Animal class and implements Pet interface.
b. This class must include a String attribute to store the name of the pet.
c. Define a constructor that takes one String parameter that specifies the cat's name. This constructor must also call the superclass constructor to specify that all cats have four legs.
d. Define another constructor that takes no parameters. Have this constructor call the previous constructor (using the this keyword) and pass an empty string as the argument.
e. Implement the Pet interface methods.
f. Implement the eat method.

5. Create the Fish class extends the Animal class.
a. The Fish class extends the Animal class.
b. This class must include a String attribute to store the name of the pet.
c. Define a no-arg constructor that calls the superclass constructor to specify that fish do not have legs.
d. implement the Pet interface methods.
e. Override the walk method. Thism ethod should call the super method and they print a message that fish do not walk.
f. Implement the eat method.

Task 2 - Creating the TestAnimals Program

Create a TestAnimals program. Have the main method create and manipulate instances of the classes you created above.

Start with:

Fish d = new Fish();
Cat c = new Cat("Fluffy");
Animal a = new Fish();
Animal e = new Spider();
Pet p = new Cat();

Experiment by:

Task 3 - Compiling the TestAnimals Program

On the command line, use the javac command to compile the test program.

Task 4 - Running the Testanimals Program

On the command line, use the java command to run the test program.