2021年3月7日星期日

confused with creation of subclasses - AP comps preparing [duplicate]

I have a BankAccount class which is superclass and SavingsAccount and CheckingAccounts as subclasses/derived classes. Very simple classes. Preparing for AP computer science.

   BankAccount b = new BankAccount(100);     BankAccount s = new SavingsAccount(100);     BankAccount c = new CheckingAccount(200);  

Question 1: When I try to invoke a method that is in Savingsaccount, it throws an error saying method not available. Question 2: Why even have BankAccount on left-hand side for SavingsAccount and CheckingAccount?

  public class BankAccount {    private double balance;    public BankAccount() {      balance =0;  }    public BankAccount(double acctBalance){        balance = acctBalance;    }    public void deposit (double amount){        balance = balance + amount;  }    public void withdraw(double amount) {        balance = balance - amount;  }       public double getBalance() {        return balance;    }   }          public class SavingsAccount extends BankAccount{             private double interestRate;             public SavingsAccount() {               }    public SavingsAccount(double acctbalance, double rate){         super(acctbalance);    }      public void addInterest() {      }   }       public class CheckingAccount  extends BankAccount{    public CheckingAccount(double acctBalance) {    }       public void withdraw(double amount){      }  }  
https://stackoverflow.com/questions/66523150/confused-with-creation-of-subclasses-ap-comps-preparing March 08, 2021 at 09:04AM

没有评论:

发表评论