Calling Overridden method from derived class

I have two objects derived from an Account class stored in an ArrayList. I am trying to call overridden methods with a basic for loop but I can't get it to work consistently.

import java.util.ArrayList;
import java.util.Scanner;

public class InheritanceTest 
{

private static class Account
{

    //Constructor
    Account()
    {

    }

    private void processDeposit()
    {


        double deposit = 0;

        System.out.println("Please enter the deposit amount: ");
        deposit = input.nextDouble();

        balance += deposit;     
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();
        balance -= withdrawal;

    }

    public String getAccountName() {
        System.out.println("Creating " + "account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public int getAcctID() {
        System.out.println("Your " + "account number is " + acctID);
        return acctID;
    }

    public void setAcctID(int acctID) {
        this.acctID = acctID;
        acctID++;
    }

}

//

 private static class CheckingAccount extends Account
{

    public String getAccountName() {
        System.out.println("Creating checking account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();

        if (withdrawal > balance)
        {
            balance -= 10;
            System.out.println("Overdraft fee has been charged");
        } else
            balance -= withdrawal;

    }

}

//

 private static class SavingsAccount extends Account
{

    public String getAccountName() {
        System.out.println("Creating savings account, enter name: ");
        accountName = input.nextLine();
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    private void processWithdrawal()
    {
        double withdrawal = 0;

        System.out.println("Please enter the amount to be drawn from the account: ");
        withdrawal = input.nextDouble();
        if(withdrawal > balance)
        {
            System.out.println("Insufficient funds");
        } else
        {
            balance -= withdrawal;
        }

    }


    }

//

public static void main(String[] args)
{
    ArrayList<Account> Bank = new ArrayList<Account>();
    CheckingAccount checking1 = new CheckingAccount();
    Bank.add(checking1);
    SavingsAccount savings = new SavingsAccount();
    Bank.add(savings);

    for(int i = 0; i<Bank.size(); i++)
    {
        Bank.get(i).getAccountName();
        Bank.get(i).setAccountName(Bank.get(i).accountName);
        Bank.get(i).getAcctID();
        Bank.get(i).setAcctID(Bank.get(i).acctID);
    }

    for (int i =0; i<Bank.size(); i++)
    {
        System.out.println("Processing Account: " + Bank.get(i).acctID);
        Bank.get(i).processDeposit();
        System.out.println("Balance: " + Bank.get(i).balance);
        Bank.get(i).processWithdrawal();
        System.out.println("Balance: " + Bank.get(i).balance);
        Bank.get(i).processWithdrawal();
        Bank.get(i).displayAccount(Bank.get(i).acctID, Bank.get(i).accountName, Bank.get(i).balance);

    }
}

}

The first loop calls the overridden getAccountName methods just fine, yet the second loop only seems to be calling the processWithdrawal method from the base class. Can anyone point me in the direction of finding out why this might be happening?

2 Answers
  1.  private void processWithdrawal()
    

    make it public

     public void processWithdrawal()
    
    2015-11-25 11:32:03
  2. When you would like to override a method i.e. leverage the feature of Dynamic Polymorphism the method should be protected/public.

    A private method is restricted only to that specific class in which it is declared which wouldnt make Overriding possible.

    user38447442015-11-25 12:14:56
Related Articles
You Might Also Like