Computing Concepts with Java Essentials
Laboratory Notebook
Chapter 9 - Inheritance and Interfaces


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 year
class Card
{  public Card()
   {  name = "";
   }
   public Card(String n)
   {  name = n;
   }
   public boolean isExpired()
   {   return false;
   }
   public void print()
   {   System.out.println("Card holder: " + name);
   }
   private String name;
}

Write definitions for each of the subclasses. For each subclass, supply private instance variables. Leave the bodies of the constructors and the print methods 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. Here is one example:

IDCard(String name, int id)
{  super(name);
   idNumber = id;
}

P3. Dynamic Binding

Supply the implementation of the print method 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 slots for two cards, card1 and card2, a method addCard(Card) and a method method printCards().

The addCard method checks if card1 is null. If so, it sets card1 to the new card. If not, it checks card2. If both cards are already set, the method has no effect. (You will see in chapter 11 how to collect an arbitrary number of items.)

Of course, printCards invokes the print method on each non-null card.

Have a main method 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 method DriverLicense.isExpired() that checks if the driver license is already expired (i.e. the expiration year is less than the current year).

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

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 method.

Show the output of your test run.


P4. Interfaces

Implement the Comparable interface for the Card class. You need to supply a clause

class Card implements Comparable

and a method

int compareTo(Object other)

Compare two Card objects by comparing their name fields.


P5. Overriding methods from the Object class

Implement the following three methods for the Card class:

The toString method should return a string like this:

Card[name=...]

The equals method should return true if two cards have the same name.

The clone method should return a new Card object with the same name.


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