implements
and extends
. In this assignment we will use Old MacDonald's Farm to learn how
they work,
and the meaning of inheritance and polymorphism.
interface
defines those things required to be an animal on the farm.
public interface Animal
{
public String getSound();
public String getType();
}
Note: Since this interface is public
, it should be in it's own java source file. In RealJ, choose File |
New | Java Source File. After saving the file as Animal.java
, add it to your project by choosing
Project | Add Java source files. . .
Once we know what it takes to be an Animal, we can define new classes for the cow, chick and pig that implement the 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;
public Cow(String type, String sound)
{
myType = type;
mySound = sound;
}
public Cow()
{
myType = "unknown";
mySound = "unknown";
}
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:
public class ApJava8 extends Applet
{
public void paint(Graphics g)
{
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 class
source code.
class Farm
{
private Animal[] aBunchOfAnimals = new Animal[3];
Farm()
{
aBunchOfAnimals[0] = new Cow("cow","moo");
aBunchOfAnimals[1] = new Chick("chick","cluck");
aBunchOfAnimals[2] = new Pig("pig","oink");
}
public void animalSounds()
{
for (int nI=0; nI < aBunchOfAnimals.length; nI++)
{
System.out.println( aBunchOfAnimals[nI].getType() + " goes " + aBunchOfAnimals[nI].getSound() );
}
}
}
You will need to change your 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 class
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 class
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
code to exercise all your modifications is
shown here:
class Farm
{
private Animal[] aBunchOfAnimals = new Animal[3];
Farm()
{
aBunchOfAnimals[0] = new NamedCow("cow","Elsie","moo");
aBunchOfAnimals[1] = new Chick("chick","cheep","cluck");
aBunchOfAnimals[2] = new Pig("pig","oink");
}
public void animalSounds()
{
for (int nI=0; nI < aBunchOfAnimals.length; nI++)
{
System.out.println( aBunchOfAnimals[nI].getType() + " goes " + aBunchOfAnimals[nI].getSound() );
}
System.out.println( "The cow is known as " + ((NamedCow)aBunchOfAnimals[0]).getName() );
}
}
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 aBunchOfAnimlas[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.
Submit the Java code (the .java
file(s)) in an email message
to mrsimon@lycos.com. It is not necessary to post your applet on your website for this assigment.