Computing Concepts with Java Essentials
Laboratory Notebook
Chapter 14 - Inheritance and Polymorphism

Cay S. Horstmann
Geof Pawlicki

Your name:
Your email address:
Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience with

P1. Inheritance

Consider using the following Card class as a superclass to implement a hierarchy of related classes:

Class Data
IDcard ID number
Calling Card Card number, PIN
DriverLicense Expiration date
class Card
{  public Card() 
   {  name = "";
   public Card(String n)
   {  name = n;
   public boolean isExpired() 
   {   return false;
   public void print() 
   {   System.out.print(name);  
   private String name;

Write definitions for each of the subclasses. For each subclass, supply private data members. Leave the bodies of the constructors and the print functions blank for now.

P2. Calling the Superclass constructor

Implement constructors for each of the three subclasses. Each constructor needs to call the superclass constructor to set the name.

P3. Dynamic Binding

Supply the implementation of the print function for the three subclasses. The subclass methods need to call the superclass print method to print the name of the cardholder.

Devise another class, Billfold, which contains an array Card[] cards, a method addCard(Card) and a method function printCards(). Of course, printCards invokes the print method on each card.

Have a main function populate a Billfold object with Card* objects, and then call print_cards.

Show the output of your test run.

Explain why the output shows that the call to print uses dynamic binding.

The Card superclass defines a method isExpired which always returns false. This method is appropriate for the ID card and the phone card because those cards don't expire. But it is not appropriate for the driver license. Supply a member function DriverLicense.isExpired() that gets the current time and checks if the driver license is already expired.

Note that you should not redefine isExpired for the other card types. They simply inherit the superclass function.

Add a method printExpiredCards to the Billfold which queries each card whether it is expired and only prints those that are expired.

Write a main program that populates a billfold with an ID card, a phone card and an expired driver license. Then call the printExpiredCards function.

Show the output of your test run.

Don't forget to send your answers when you're finished.