home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
lab: inheritance intro
Objectives
After successfully completing this assignment you will be able to create and use a class
hierarchy with super classes and subclasses.
Your code will be similar to
lecture's example code
(even the same subject-topic),
but go ahead and write the files entirey yourself, rather than copy/pasting anything.
Assignment
You will implement the class hierarchy shown below and create a farm that contains many
different types of animals. You will create
a
class Animal that
represents common properties and operations of all animals.
For this lab: we will only be dealing with
the classes
Animal,
Pig,
Horse.
Animal
Create a class named Animal.
All animals have a name, eat food, and make a sound.
Create instance variables for name, food, and sound.
The constructor will take three parameters: a value for name, food, and sound.
private vs protected:
We've learned to make fields private,
and make most methods public (including, possibly, getters and setters).
However, if an Animal field is private,
then it can't be used by any other classes…including subclasses like
Pig!
Java's solution: declare your fields to be protected
—
meaning, private to this class and any class that extends it
.
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.
A Pig named Wilbur would be described with
"Wilbur eats slop says \"oink\""
(that is, a string that includes quote-marks around the sound).
Pig
Create a subclass of Animal named Pig. Recall that a subclass
extends a parent class
(and thus 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.
Making a class abstract
Eventually, we will be making new Pigs and new Horses, etc..
However, it doesn't make sense to say
make a new Animal, without it being a particular (sub)type of Animal
.
To tell Java that the Animal constructor should not be called,
add the word abstract to its class-declaration: abstract class Animal {…}.
The keyword abstract is used to mean
It’s not conceivable to have an object of this class that is not actually some subtype of this class.
.
(Though the only thing the Java language needs to enforce about this is that it won't let you call the constructor of
an abstract class directly;
it can only get called indirectly through a subclass's super(…).
Note:
It's certainly possible to extend a class, even if it's not abstract.
We'll see in lecture next time, that
we'll have a (non-abstract) class Dog,
as well as a class Watchdog extends Dog which has some extra/different
fields/methods from regular, non-watchDog.
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
neigh
. Therefore, the constructor calls super with the name of the horse,
"hay", and "neigh". 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.
Make a method toString which includes the horse-type at the beginning
(e.g. "The race horse Secretariat eats hay says \"neigh\"".)
Farm
Let's expand the farm to include a couple of horses, and to print some information.
Add the following to Farm.main:
-
Instead of creating separate variables for each animal:
build a pen — an array of Animal — to hold all of the animals:
Animal[] pen = new Animal[3];.
- Put Wilbur in the first location of the pen.
- Create a talking-horse named
Mr. Ed
and put Mr. Ed in the second location of the pen.
- Create a race-horse named
Secretariat
and put Secretariat in the third location of the pen.
-
Notice that: each location in the array pen holds an object of type Animal,
which enables the pen to hold any particular kind of Animal
(Pig extends Animal and Horse extends Animal).
-
Write a for-each loop that prints each animal in the pen.
This doesn't need to be its own method, for expediency only.
-
Print the sounds made by each animal in the pen (separated by a comma; see sample-output below).
Make this a method which is takes in Animal[] and returns one big String;
Farm.main will call that helper and will println the String it returns.
This is consistent with our general goal of using helpers, but restricting I/O to just the top (few) “top-level” function(s).
-
Finally, move the helper-method that returns all sounds of an Animal[]
from class Farm into the most appropriate class.
Output
When your program is complete, Farm.main's output should look like the following:
Welcome to the Pittges Farm.
Let me introduce you to the animals:
Wilbur eats slop says "oink"
The talking horse Mr. Ed eats hay says "neigh"
The race horse Secretariat eats hay says "neigh"
Listen to all of the animals: moo, neigh, neigh
Thank you for visiting our farm.
Submit Your Assignment
Submit all your .java source files on D2L.
credit:
Originally
written by
Jeff Pittges and Shawn Brenneman;
modified by permission.
home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |