ITEC 120
L13b: Inheritance

Objectives

After successfully completing this assignment you will be able to create and use a class hierarchy with super classes and subclasses.

Assignment

You will implement the class hierarchy shown below and create a farm that contains many different types of animals. You will create an abstract class named Animal that represents common properties and operations of all animals. You will create a second abstract class named Producer that represents animals that produce something of value, and you will create subclasses that represent specific animals.

Animal

Create an abstract class named Animal. All animals have a name, eat food, and make a sound. Create protected instance variables for name, food, and sound. The constructor will take three parameters: a value for name, food, and sound.

Create a method named speak that returns the sound the animal makes. Create a stub method named eat that takes an array of strings and returns nothing. Create a toString method that prints the animal's name, sound, and food. The string below describes a pig named Wilbur.

Wilbur eats slop says oink

An abstract class may not be instantiated. Therefore, we must create at least one subclass before we may start testing.

Pig

Create a subclass of Animal named Pig. Recall that a subclass extends a parent class and inherits the parent's instance variables and methods. The constructor takes one parameter, the name of the pig. All pigs eat slop and say oink. Therefore, the constructor calls super, the parent's constructor, with the name, "slop", and "oink".

This completes the entire Pig class. This may not seem like much, but as you will soon see, the Pig class illustrates the power of inheritance. 

Farm

Create a driver class named Farm. In the main method, create a variable named pig of type Animal, create a new instance of a Pig named Wilbur, and print the variable pig.

Animal pig = new Pig("Wilbur");
System.out.println(pig);

How is it possible to create a variable of type Animal and assign the variable to point to a Pig? The Pig class is a subclass of Animal. Therefore, a Pig is a Animal and any variable of type Animal may point to any subclass of Animal.

Horse

Create a subclass of Animal named Horse. A Horse inherits all of the properties of an Animal, but a Horse has an additional property, type, that represents a type of horse such as a show horse, a riding horse, or a workhorse. Create a protected instance variable named type.

The constructor takes two parameters: the name and type of the horse. All horses eat hay and say nay. Therefore, the constructor calls super with the name of the horse, "hay", and "nay". When super returns, the constructor initializes the type of horse. Always call the parent constructor first and then implement any specializations of the given class.

Farm

Let's extend the farm to include two horses. Instead of creating separate variables, create a pen, an array of Animal, to hold all of the animals.

Animal[] pen = new Animal[3];

Put Wilbur in the first element of the pen. Create a show horse named Mr. Ed and put Mr. Ed in the second element of the pen. Create a racing horse named Secretariat and put Secretariat in the third element of the pen. Note that each element in the array pen is an Animal, which enables the pen to hold any particular kind of animal. (A pig is an animal, a horse is an animal.)

Write a For Each loop that prints each animal in the pen. Write a second loop that prints the sounds made by each animal in the pen.

Producer

Create an abstract subclass of Animal named Producer. The Producer subclass rep­resents animals that produce something valuable. Create an instance variable named gives (String) to hold a description of what the animal produces.

Create a constructor that takes four parameters: values for name, eats, sounds, and gives. The constructor calls super with the name, eats, and sound before initializing gives.

The new property gives is not included in the toString method in the Animal class. The Producer class will override the toString method by creating a method specific to Producers. Write a toString method that uses super to call the parent toString method and then adds gives to the string. The example below describes a cow named Bessie.  

Bessie eats hay says moo gives milk

Cow

Create a subclass of Producer named Cow. A Cow inherits all the properties of a Pro­ducer. All cows eat hay, say moo, and give milk. Create a constructor that takes one parameter, the name of the cow, and calls the Producer constructor with the name, "hay", "moo", and "milk".

Extend the farm by adding a cow named Bessie to the pen. Be sure to change the size of the pen to 4.

Sheep

Create a subclass of Producer named Sheep. All sheep eat clover, say bah, and give wool. Create a constructor that takes one parameter, the name of the sheep, and calls the parent constructor with the name, "clover", "bah", and "wool". Add a sheep named Dolly to the farm.

Goat

Create a subclass of Producer named Goat. All goats say neiiigh, give milk, and they will eat anything. Create a constructor that takes the name of the goat and calls the parent with the name, "anything", "neiiigh", and "milk". Add a goat named Billy to the farm.

 

Feeding the Animals

Now that you have developed a fairly robust farm you must feed the animals by imple­menting the eat method in the Animal class. The method takes an array of strings where each string represents an item on the farm. Each animal goes through the array looking for something it can eat. If the animal finds something to eat that item is set to the empty string "" and the method prints the name of the animal and what the animal ate. If the animal does not find anything to eat the method prints that the animal ate nothing (see below).

Bessie ate hay
Mr. Ed ate nothing

Implement the eat method in the Animal class and add a loop to the end of the Farm driver that feeds each animal in the pen by calling the eat method. Each subclass of Animal inherits the eat method. Test the driver with the food shown below.

String[] = {"slop", "hay", "clover", "shoes"};

The eat method works for all of the animals except the goat. Modify your program such that Billy eats the shoes.

Output

When your program is complete the output should look like the following:

Welcome to the Pittges Farm.

Let me introduce you to the animals:

Bessie eats hay says moo gives milk
Mr. Ed, a show horse, says nay eats hay
Secretariat, a race horse, says nay eats hay
Dolly eats clover says bah gives wool
Wilbur eats slop says oink
Billy eats anything says neiiigh gives milk

Listen to all of the animals: moo, nay, nay, bah, oink, neiiigh

Let's feed the animals

Bessie ate hay
Mr. Ed ate nothing
Secretariat ate nothing
Dolly ate clover
Wilbur ate slop
Billy ate shoes

Thank you for visiting our farm.

 

Submit Your Assignment

Submit your Java source code files to the L13b dropbox on D2L.