0

here is the sample code


public class BankAccount {
    protected double getBalance() {return 1000.0;}
    }
package test;

public class SavingAccount extends BankAccount{
    protected double getBalance() {return 1010.0;}
    protected void printBalance() {
    System.out.println(super.getBalance());
    System.out.println(getBalance());
    System.out.println(this.getBalance());
    }
}

Super.getBalance() returns the value of the parent class, so it is 1000.0 this.getBalace() returns the value of the current object, so it is 1010.0 but I am not sure how getBalance() works. shouldn't it be the same as this.getBalance()? also how do I run it to see the result? I am using eclipse. Didn't find out how to run it without a main. and can't just creat a main like

public void main (String[] args) {
        SavingAccount.printBalance();
    }

because "Cannot make a static reference to the non-static method printBalance() from the type SavingAccount"

Tony Lau
  • 75
  • 6
  • Does this answer your question? [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – dnault Jul 07 '20 at 23:12

1 Answers1

0

printBalance is an instance method. You need to instantiate a SavingAccount object in order to call it. e.g.

public static void main(String[] args) {
    SavingAccount sa = new SavingAccout();
    sa.printBalance();
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350