SAFE Conversions between Related Classes:

  1. class SavingAccount extends class BankAccount:

  2. Consider a BankAccount and a SavingAccount object:

    
          BankAccount myBankAccount = new BankAccount();
          SavingsAccount mySavingsAccount = new SavingsAccount(...);   
    
    

    Situation in computer memory:

  3. Consider the following conversions:

  4. WHY is automatic convesrion from child class to parent class so important & useful:

    Example: BankAccount class with "transfer" method
    
        public class BankAccount
        {
          private double balance;
    
          public void deposit(double amount)
          {
             balance = balance + amount;
          }
    
          public void withdraw(double amount)
          {
             if ( amount <= balance )
                balance = balance - amount;
             else
                System.out.println("Insufficient funds to withdraw $" + amount);
          }
    
          public double getBalance()
          {
             return(balance);
          }
    
          // Transfer "amount" from "this" account to "other" account
          public void transfer(BankAccount other, double amount)
          {
             withdraw(amount);
             other.deposit(amount);
          }
       }
    

    Example usage:

    Example: transfering $1000 from momsChecking to harrysChecking
       public class myTestProgram
       {
          public static void main(String args[])
          {
             BankAccount harrysChecking = new BankAccount....;
             BankAccount momsChecking   = new BankAccount....;
    
             momsChecking.transfer(harrysChecking, 1000);
          }
       }
    

    What automatic conversion from SavingsAccount to BankAccount will do for SavingsAccount objects:

    Definition of SavingsAccount class is UNCHANGED:
       public class SavingsAccount extends BankAccount
       {
          double interestRate;
    
          public SavingsAccount(double rate)
          {
             interestRate = rate;
          }
    
          public void addInterest()
          {
             double interest;
    
             interest = getBalance() * interestRate/100;
             deposit(interest);
          }
    
          // NOTE that SavingsAccount has INHERITED
          //
          //   public void transfer(BankAccount other, double amount) !!!
    
       }
    
    

    Java allows us to use transfer() in paraent class BankAccount with SavingsAccount (child class) objects:

    Example:
       public class myTestProgram
       {
          public static void main(String args[])
          {
             BankAccount harrysChecking  = new BankAccount....;
             BankAccount momsChecking    = new BankAccount....;
             SavingsAccount harrysSaving = new SavingsAccount....;
             SavingsAccount momsSaving   = new SavingsAccount....;
    
             // Note: transfer expects BankAccount object parameters
             // for parameter variables "this" and "other"
    
             momsChecking.transfer(harrysChecking, 1000); 
                          // Calls transfer() in BankAccount
                          // OK, harrysChecking is a BankAccount
    
             momsChecking.transfer(harrysSaving, 1000); 
                          // Calls transfer() in BankAccount
                          // OK, converts harrysSaving to BankAccount
    
             momsSaving.transfer(harrysChecking, 1000); 
                          // Calls transfer() in SavingsAccount
                          // OK, harrysChecking is a BankAccount
    
             momsSaving.transfer(harrysSaving, 1000); 
                          // Calls transfer() in SavingsAccount
                          // OK, converts harrysSaving to BankAccount
          }
       }
    

  5. DEMO Programs: