CS 214 Lab 10: Inheritance and Polymorphism


Inheritance is a language feature by which a new type can be declared that extends the capabilities of an existing type. Polymorphism is a language feature that allows the same function call to be associated with different definitions during the same program's execution, by delaying the binding of the call to run-time. This week, we examine the capabilities our languages provide for inheritance and polymorphism.

Inheritance is usually accomplished by deriving a new class from an existing class. The language must provide some mechanism for specifying that the new class inherits the attributes of its parent class.

Because of our time constraints, we will examine a very simple problem: defining Duck, Goose and Owl objects, and having each display its name and its call.

Object-Oriented Analysis. In examining our problem, we can identify three basic objects:

  1. A Duck object,
  2. A Goose object, and
  3. An Owl object.
Since such objects are not directly representable using the available types, we will build classes to represent such objects.

Each of these objects has several attributes, including

Note that the name of a Duck (or Goose or Owl) will vary from Duck to Duck (e.g, Daffy, Donald, Daisy, etc.). Since every Duck's name can be different, a Duck must remember its name -- space will be required to store the Duck's name.

By contrast, every Duck (or Goose, or Owl) has the same call. Since every Duck's call is the same, no storage is needed for its call -- we can simply provide a function member (i.e., method) that returns the same bird call for all Ducks (or Geese or Owls).

Object-Oriented Design. Once we have identified the objects and their operations, object-oriented design is to identify common data and/or operations, and design a class hierarchy that consolidates commonalities.

For example, every Duck, Goose and Owl needs space to store their name, so this is a common data item. Since each of our objects has this in common, we can either

This latter approach takes a bit more design time, but will save us coding time because we will be avoiding redundant work in each of our three derived classes. Ducks, Geese and Owls are all birds, so we might design a Bird class in which to store their common attributes.

A Duck, Goose and Owl will also need accessor functions to retrieve their name, as well as some means of setting their name. Since this functionality is common to all three of our classes, it should also be isolated in our Bird class.

Similarly, we will need to be able to determine the bird call of a Duck, Goose and Owl. However, a Duck's bird call is different from a Goose's bird call, which is different from an Owl's bird call. Each derived class will thus need to provide the functionality for its particular call. For completeness, we will place a default bird call in our Bird class.

Finally, we must be able to display the name of a Duck, Goose or Owl and its bird call. Since we must do the same thing for each, we can avoid redundant coding by defining an output function in the parent class that does this. However, the call of each bird is different, so an output function in the parent class must reference the bird-call functions of the derived classes. This is polymorphic behavior, which we will examine in our languages.

Combining all of these observations gives us the following hierarchy:

a Bird hierarchy

Today's exercise to implement this design in Java, Ada, Clojure, and Ruby.

As usual, "skeletons" are provided to save you some of the work. The exercises will compare the implementations of this hierarchy in our four languages.

Begin by making a new directory for this lab exercise and changing directory to that new directory.

  1. Java Intro
  2. Ada Exercise
  3. Clojure Exercise
  4. Ruby Exercise

Turn in. Each of the four parts requires you to create files containing source code and execution traces. When you have completed them, use cat to create a single file that contains all of your results:

   cat java/script.java ada/script.ada clojure/script.clojure ruby/script.ruby > lab10-results
Then submit your work by copying that single file into your personal folder in /home/cs/214/current/:
   cp lab10-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name. The grader will access and grade your results from there.


Calvin > CS > 214 > Labs > 10


This page maintained by Joel Adams.