Inheritance Hierarchy in Java

  1. In Object Oriented Programming, it is a common practice to group classes into a inheritance hierarchy

    Classes at the top level are very general in nature & contain general/common functions

    Classes at the lower levels are more specific & contain specialized functions (for a specialized task)

    Example: Class hierachy in Java for Graphic Programming


    We use the following simpler Bank account hierarchy to study the inheritance concept

  2. Design Information:

    A bank offers 3 different kinds of accounts to customers:

    1. CheckingAccount:
      • No interest
      • A small number of free transactions per month
      • Transaction: deposit() or withdraw() (balance inquiry is free)
      • Charge a fee for each additional transaction beyond the limit

    2. SavingAccount:
      • interest compounded monthly

    3. TimeDepositAccount:
      • must specify length of deposit period
      • interest compounded monthly
      • Charge a penalty for early withdrawal

  3. Class Designs:

    1. CheckingAccount:
      • void deposit(double amount):
        • add amount to balance
        • add 1 to transactionCount
      • void withdraw(double amount):
        • subtract amount from balance
        • add 1 to transactionCount
      • double getBalance():
        • return balance
      • void deductFees():
        • if transactionCount > MAXALLOWED then deduct fees.

    2. SavingAccount:
      • void deposit(double amount):
        • add amount to balance
      • void withdraw(double amount):
        • subtract amount from balance
      • double getBalance():
        • return balance
      • void addInterest():
        • add interest to balance

    3. TimeDepositAccount:
      • void deposit(double amount):
        • add amount to balance
      • void withdraw(double amount):
        • subtract amount from balance
        • subtract penalty from balance for early withdrawal
      • double getBalance():
        • return balance
      • void addInterest():
        • add interest to balance
        • update deposit time remaining


    The Traditional Solution:


  4. The Object Oriented Approach:

  5. Prime Directive in Designing Inheritance Hierarchy:

    Example of a poor Inheritance Hierarchy design:

  6. Possible Inheritance Hierarchy:

  7. Class behavior analysis: Determine if methods are unsuitable

    Bank Account Class Hierarchy:

  8. Conclusion: to allow RE-USE of existing software through inheritance, we need:

    So before we continue with the BankAccount hierarchy design, let's first look at the effect of overriding methods