Lab 1370 - Old MacDonald

Description

In this lab, we use Old MacDonald's Farm to learn about Inheritance and Polymorphism.

Defining an Interface

Old MacDonald had a farm that had several types of animals. Every animal shared certain characteristics: they had a type (such as cow, chick or pig) and each made a sound (moo, cluck or oink). An Interface defines those things required to be an animal on the farm.

public interface Animal
{
   public String getSound();    
   public String getType();
}

Once we know what it takes to be an Animal, we can define new classes for the cow, chick and pig that implement to Animal interface. Here is a Cow class meeting the minimum requirements to be an Animal.

class Cow implements Animal
{
    private String myType;
    private String mySound;

    Cow(String type, String sound)
    {
        myType = type;
        mySound = sound;
    }

    public String getSound() { return mySound; }
    public String getType() { return myType; }
}

Your turn: Implement classes for the chick and the pig. Also complete the test program below to verify your work so far:

class TestFarm {

   public static void main(String[] args)
   {
      Cow c = new Cow("cow", "moo");
      System.out.println( c.getType() + " goes " + c.getSound() );
      < your code here >
   }
}

Now create a complete farm to test all your animals. Here is the Farm.java source code.

class Farm {

   private Animal[] a = new Animal[3];
   Farm() {
      a[0] = new Cow("cow","moo");
      a[1] = new Chick("chick","cluck");
      a[2] = new Pig("pig","oink");
   }

   public void animalSounds()
   {
      for (int i=0; i < a.length; i++)
      {
         System.out.println( a[i].getType() + " goes " + a[i].getSound() );
      }
   }
}
You will need to change your TestFarm.java code to create an object of type Farm and then to invoke its animalSounds method.

Turns out, the chick is a little confused. Sometimes it makes one sound, when she is feeling childish, and another when she is feeling more grown up. Her two sounds are "cheep" and "cluck". Modify the Chick.java code to allow a second constructor allowing two possible sounds and the getSound() method to return either sound, with equal probability, if there are two sounds available. You will also have to modify your Farm.java code to construct the Chick with two possible sounds.

Finally, it also came to pass that the cows get a personal name, like Elsie. Create a new class, NamedCow, that extends the cow class, adding a constructor, a field for the Cow's name, and a new method: getName. The final Farm.java code to exercise all your modifications is shown here:

class Farm {

   private Animal[] a = new Animal[3];
   Farm() {
      a[0] = new NamedCow("cow","Elsie","moo");
      a[1] = new Chick("chick","cheep","cluck");
      a[2] = new Pig("pig","oink");
   }

   public void animalSounds()
   {
      for (int i=0; i < a.length; i++)
      {
         System.out.println( a[i].getType() + " goes " + a[i].getSound() );
      }
      System.out.println( "The cow is known as " + ((NamedCow)a[0]).getName() );
   }
}
What Did You Just Do?

So you don't miss it, make sure you understand what you just accomplished. Having an array of Animal objects and then having the getSound() method dynamically decide what sound to make is polymorphism. This is also known as late binding because it wasn't known until run-time that a[1], for example, really had a Chick object.

You started with an Interface for an Animal and then used the keyword implements in making the three types of animals. Then you created a specialized version of the Cow, a NamedCow, using the keyword extends. This illustrates the concept of inheritance. The NamedCow had all the attributes and methods of the Cow and then added some: a new field and a new method to access the cow's name.


Copyright © 2010 by Asylum Computer Services LLC Return to CS Labs