Polymorphism

  1. You have seen polymorphism before: click here

    ....Methods defined in the PARENT class (and inherited by the child class) that invoke the overridden method deposit(), will NOW ALSO invoke the deposit() method defined inside the child class...

    This concept is very important and deserve to be highlighted

  2. You have seen the following example:

    Example:
       public class BankAccount
       {
          public double balance;
    
          public void CALLdeposit(double amount)
          {
             System.out.println("Invoking BankAccount's CALLdeposit()....");
             deposit(amount);
          }
    
          public void deposit(double amount)
          {
             System.out.println("Invoking BankAccount's deposit()....");
             balance = balance + amount;
          }
    
          public double getBalance()
          {
             System.out.println("Invoking BankAccount's getBalance()....");
             return(balance);
          }
       }
    
       public class CheckingAccount extends BankAccount
       {
          int transactionCount;
    
          public void deposit(double amount)
          {
             System.out.println("Invoking CheckingAccount's deposit()...");
    
             balance = balance + amount;
    	 transactionCount++;
          }
          ....
       }
    
       public class Demo
       {
          public static void main(String args[])
          {
             BankAccount a = new BankAccount();
             CheckingAccount b = new CheckingAccount();
    
             a.CALLdeposit(10);   // CALLdeposit invokes BankAccount's deposit
       
             b.CALLdeposit(10);   // CALLdeposit invokes CheckingAccount's deposit
    
    	 a = b;
             a.CALLdeposit(10);   // CALLdeposit invokes CheckingAccount's deposit
    
          }
       }
    

    What is so special about this:

  3. The reason why:

  4. Another Example:

    BankAccount class
       public class BankAccount
       {
          private double balance; 
    
          public BankAccount()
          {  balance = 0;
          }
    
          public BankAccount(double initialBalance)    
          {  balance = initialBalance;
          }
     
          public void deposit(double amount) 
          {  balance = balance + amount;
          }
    
          public void withdraw(double amount) 
          {  balance = balance - amount;
          }
    
          public double getBalance()
          {  return balance; 
          }
    
          public void transfer(BankAccount other, double amount)   
          {  
             withdraw(amount);
             other.deposit(amount);  // <--- Calls a deposit() method !!!
          }
       }
    
       public class CheckingAccount extends BankAccount
       {
          int transactionCount;
    
          public void deposit(double amount)
          {
             System.out.println("Invoking CheckingAccount's deposit()...");
    
             balance = balance + amount;
    	 transactionCount++;
          }
          ....
       }
    
       public class Demo
       {
          public static void main(String args[])
          {
             BankAccount z = new CheckingAccount();
    
             BankAccount a = new BankAccount();
             CheckingAccount b = new CheckingAccount();
    
    
             z.transfer(a,10); // transfer() invokes a.deposit(10)
    			   // which will invoke 
    			   // BankAccount's deposit()
       
             z.transfer(b,10); // transfer() invokes b.deposit(10)
    			   // which will invoke 
    			   // CheckingAccount's deposit()
    
          }
       }
    

  5. This example affirms the principle that: