Java Reference
In-Depth Information
balance, but has a lower interest rate. Similarly, you may have a checking account that limits
the number of checks that you can write each month. Another type of account that is used to
save money for the long term is a certificate of deposit (CD). To illustrate how abstract classes
are designed and how polymorphism works we assume that the bank offers three types of
accounts—savings, checking, and certificate of deposit, as described next.
Savings accounts: Suppose that the bank offers two types of savings accounts: one that
has no minimum balance and has a lower interest rate and another that requires a
minimum balance and has a higher interest rate.
Checking accounts: Suppose that the bank offers three types of checking accounts: one
with a monthly service charge, a limited number of monthly check writing, no minimum
balance, and no interest; another with no monthly service charge, requires a minimum
balance, allows an unlimited number of monthly check writing, pays lower interest; and a
third with no monthly service charge, requires a higher minimum balance, has a higher
interest rate, and allows an unlimited number of monthly check writing.
Certificate of deposit (CD): In an account of this type, money is left for some time and
these accounts draw higher interest rates than either savings or checking accounts.
Suppose that you purchase a CD for six months. Then we say that the CD will mature
in six months. Furthermore, the penalty for early withdrawal is stiff.
Figure 10-7 shows the inheritance hierarchy of these bank accounts.
BankAccount
CheckingAccount
CertificateOfDeposit
SavingsAccount
HighInterestSavings
ServiceChargeChecking NoServiceChargeChecking
HighInterestChecking
FIGURE 10-7 Inheritance hierarchy of bank accounts
Note that the classes BankAccount and CheckingAccount are abstract. That is, we
cannot instantiate objects of these classes. Typically, common characteristics are placed as
high as possible in the inheritance hierarchy and these characteristics are inherited by the
subclasses. The other classes in Figure 10-7 are not abstract. Next we describe each of
these classes in more detail.
BankAccount : Every bank account has an account number, the name of the owner,
and a balance. Therefore, instance variables name , accountNumber , and balance are
 
Search WWH ::




Custom Search