Laboratory Exercises For Computer Science 223

Inheritance in Java

This laboratory exercise provides practice defining an interface in Java, implementing that interface through a Java class, and then extending that class.

Example: Lists of Integers (as described during class)

In class, we discussed unordered and ordered lists as an example of interfaces and inheritance. The following files (found in directory ~walker/java/lists/) provide some details:

These files review various details of interfaces, implementation, and inheritance, and you may wish to use these as models for the latter parts of this lab.

  1. Copy these files to your account, compile them (in the order listed above), and then run both SimpleList and OrderedList.

  2. Compare the methods defined in with those implemented in
    1. Be sure all methods specified in are implemented.
    2. Does implement any additional public or private methods? If so, identify which new methods are defined.
    3. Temporarily delete the isin method from and recompile. Explain any error message(s) you encounter. (When you are done with this part, restore to its original form.)

  3. Review the testing section of, and describe a likely testing strategy that led to this sequence.

  4. Review, and explain how the insert method works.

  5. While testing in includes printing after each list insertion, testing in performs a sequence of insertions before printing. Do you think this abbreviated testing for is sufficiently complete? Briefly explain your answer.


During the semester, we have considered a hierarchy of appointments as part of our class project:

An Appointment Hierarchy

Using the above list programs as examples, one way to translate the appointment hierarchy into Java code would be to utilize interfaces and inheritance. In this hierarchy, Appointment could be an interface, AvailableAppt and UnchangeableAppt implementations, and the remaining three classes could extend AvailableAppt.

While folks working on appointment classes will need to work out the actual labs, this lab provides practice with some three operations: constructor, make an appointment, and print the appointment on the system's output. A sample interface is given in (This and other class files for the appointment hierarchy in this lab may be found in directory ~walker/java/schedule/.)

A simple implementation of is given by In this implementation, only the owner can adjust appointment data. Also, printing content depends upon the nature of the user.

  1. Copy and to your account, compile these classes, and run Review the code to be sure you know how it runs.

  2. Write a class AvailableAppt that implements the Appointment for appointments for which folks might sign up for meetings. (Each of these methods might be patterned on UnchangeableAppt, although editing of some text in printing may be appropriate.)

  3. Compile and test AvailableAppt.

  4. Implement classes WalkInAppt, SignUpAppt, and RestrictedAppt. These classes may inherit the printing method from AvailableAppt. Making appointments should proceed according to the following table.
    Class Owner Logged-in User Unknown User
    WalkInAppt Can make changes Told to drop in Told to drop in
    SignUpAppt Can make changes Can sign up if comment field is null or says "office hours"; otherwise told time taken Told e-mail request will be sent if comment field is null or says "office hours"; otherwise told time taken
    RestrictedApptCan make changes Told e-mail request will be sent Told e-mail request will be sent

    In your implementation (for now), you need not try to actually send e-mail. Instead, just make the appointment (assuming the owner will approve the request), but return a message indicating e-mail was sent.

  5. Test your implementations individually and thoroughly.

This document is available on the World Wide Web as

created March 14, 2002
last revised March 15, 2002
Valid HTML 3.2!
For more information, please contact Henry M. Walker at