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

In this exercise, you will  create a hierarchy of animals that is rooted in an abastract 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.

public abstract class Animal {
    //more code
}

a. Declare a protected integer attribute called legs, which records the number of legs for this animal.

protect int legs;

b. Define a protected constructor that initializes the legs attribute.

protected Animal (int legs) {
    this.legs = legs;
}

c. Declare an abstract method eat.

public abstract void eat ();

d. Declare a concrete method walk that prints out something about how the animals walks (include the number of legs).

public void walk () {
    System.ou.println("This animal walks on " + legs + "legs.");
}

2. Create the Spider class.

a. The Spider class extends the Animal class.

public class Spider extends Animal {
    //more code here
}

b. Define a no-arg constructor that calls the superclass constructor to specify that all spiders have eight legs.

public Spider () {
    super (8);
}

c. Implement the eat method.

public void eat () {
    System.out.println("The spider eats a fly.");
}

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

public interface Pet {
    public String getName();
    public void setName (String n);
    public void play ();
}

4. Create the Cat class.

a. The Cat class extends the Animal class and implements Pet interface.

public class Cat extends Animal implements Pet {
    //more code here
}

b. This class must include a String attribute to store the name of the pet.

private String name;

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.

public Cat (String n) {
    super (4);
    name = n;
}

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.

public Cat() {
    this("");
}

e. Implement the Pet interface methods.

public String getName() {
    return name;
}
public void setName (String n) {
    name = n;
}
public void play () {
    System.out.println("name + " likes to play with string.");
}

f. Implement the eat method.

public void eat () {
    System.out.println("Cats like to eat spiders and mice.");
}

5. Create the Fish class extends the Animal class.

a. The Fish class extends the Animal class.

public class Fish extends Animal implements Pet {
    //more code here
}

b. This class must include a String attribute to store the name of the pet.

private String name;

c. Define a no-arg constructor that calls the superclass constructor to specify that fish do not have legs.

public Fish () {
    super (0);     //this line must be here
}

d. implement the Pet interface methods.

public void setName (String name) {
    this.name = name;
}
public String getName () {
    return name;
}
public void play () {
    System.out.println("Fish swim in their tanks all day.");
}

e. Override the walk method. Thism ethod should call the super method and they print a message that fish do not walk.

public void walk () {
    super.walk();
    System.out.println("Fish, of course, can't walk; they swim.");
}

f. Implement the eat method.

public void eat () {
    System.out.println("Fish eat pond scum.");
}


Task 2 - Creating the TestAnimals Program

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

Here is a sample test program:

public class TestAnimals {
    public static void main(String[] args) {
        Fish f = new Fish();
        Cat c = new Cat("Fluffy");
        Animal a = new Fish();
        Aminal e = new Cat ();

        //Demostrate different implementations of an interface
        f.play();
        c.play();

        //Demostrate virtual method invocation
        e.eat();
        e.walk();

        //Demostrate calling super methods
        a.walk();
    }
}


Task 3 - Compiling the TestAnimals Program

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

javac TestAnimals.java


Task 4 - Running the Testanimals Program

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

java TestAnimals