I have this method in a 'BankAccount.java' class
public double calculateInterest()
{
double myInterest = 0.0;
if(myBalance > 0.0){
myInterest = this.myBalance * (myInterestRate/12.0);
}
return myInterest;
}
I need to use this method in my other class such as:
SavingsAccount extends BankAccount
if(this.myBalance > 0)
{
System.out.println(calculateInterest());
this.myBalance += super.calculateInterest();
this.myBalance -= this.myMonthlyServiceCharges;
}
Why cannot I do
this.myBalance += super.calculateInterest();
it is returning as 0.0
when it should be returning around 0.4
Any help would be great, thank you
it works if I put this code in my SavingsAccount class
public double calculateInterest()
{
double myInterest = 0.0;
if(myBalance > 0.0){
myInterest = this.myBalance * (myInterestRate/12.0);
}
return myInterest;
}
But it doesn't really teach me how to use the abstract class properly
Works for me. Here I reproduce with the following
public class BankAccount {
protected double myBalance = 0;
protected double myInterestRate = .6;
public double calculateInterest() {
double myInterest = 0.0;
if (myBalance > 0.0) {
double myInterestRate;
myInterest = this.myBalance * (this.myInterestRate / 12.0);
}
return myInterest;
}
}
And then....
public class SavingsAccount extends BankAccount {
double myMonthlyServiceCharges = 1;
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount();
sa.myBalance = 14;
sa.doIt();
}
void doIt() {
if (this.myBalance > 0) {
System.out.println(super.calculateInterest());
this.myBalance += super.calculateInterest();
this.myBalance -= this.myMonthlyServiceCharges;
}
}
}
Can you try that and see if it works for you?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.