Card number, PIN
definitions for each of the subclasses. For each subclass, supply private
instance variables. Leave the bodies of the
constructors and the format methods blank for now. For example,
CallingCard. Note that CallingCard does
not rewrite the isExpired method, since it is inherited from Card, and it
won't be changed. However, DriverLicense is a different matter.
P2. Calling the Superclass Constructor
Implement constructors for each of the three subclasses. Each constructor should call the superclass constructor to set the name. Here is one example: CallingCard and the driver to try it: CardDriver
Explain why the output of your program demonstrates polymorphism.
The Card superclass defines a
method isExpired, which always
returns false. This method is
not appropriate for the driver license. Supply a method
that checks if
the driver license is already expired (i.e., the expiration year is less
than the current year).
Note, you can get the current year as follows:
import java.util.Calendar ; // at the top of your program Calendar calendar = Calendar.getInstance() ; // in your method int currentYear = calendar.get(Calendar.YEAR) ;
The ID card and the calling card don't expire.
What should you do to reflect this fact in your implementation?
Add a method getExpiredCardCount, which counts the number of
expired cards in the billfold, to the
Try it out with CardDriver class that populates a billfold with a phone card and an expired driver license.
Here is a
class with a toString method
which uses the getClass() method. The purpose of this, rather than
hardcoding the name of the class as Card is so that when the
subclasses call super.toString() they will get the name of their
Add the appropriate toString() method to its three subclasses and
try it out with
The methods should print:
the name of the class
the values of all instance fields (including inherited fields)
Typical formats are:
Card[name=Edsger W. Dijkstra]