Converting Between Subclass and Superclass Types

SavingsAccount collegeFund = new SavingsAccount(10);
BankAccount anAccount = collegeFund;
Object anObject = collegeFund;

anAccount.deposit(1000); // OK
anAccount.addInterest();
// No--not a method of the class to which anAccount belongs

BankAccount anAccount = (BankAccount) anObject;

if (anObject instanceof BankAccount)
{
   BankAccount anAccount = (BankAccount) anObject;
   . . .
}


Why did the second parameter of the transfer method have to be of type BankAccount and not, for example, SavingsAccount?

   Answer: We want to use the method for all kinds of bank accounts. Had we used a parameter of type SavingsAccount,
   we couldn't have called the method with a
CheckingAccount object.


Why can't we change the second parameter of the transfer method to the type Object?

   Answer: We cannot invoke the deposit method on a variable of type Object.