Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

java - "cannot find symbol: method" but the method is declared

In my driver program, this line gives me cannot find symbol error and I don't know why. The method is clearly defined in the SavingsAccount class, and I can refer to all other methods in my driver program but just not that one, I tried changing the type to double, and etc but still not working.

Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();

SavingsAccount class inherits from Account class:

public class SavingsAccount extends Account
{
    private final short minBalance = 0;
    private double overdraftFee;
    private double yearlyInterestRate = 0.02;
    private double interestAmount;

    public SavingsAccount (String name)
    {
        super(name);
    }

    public double withdraw (double amount)
    {
        if (accountBalance - amount >= minBalance)
        {
            accountBalance -= amount;
            System.out.print ("Withdraw Successful");
        }
        else 
        {
            accountBalance -= amount;
            overdraftFee = accountBalance * (0.10);
            accountBalance += overdraftFee;
            System.out.print ("Withdraw Succesful, however overdraft fee of 10% has been applied to your account");


        }

        return accountBalance;
    }

// ----------------- this is the method I try to invoke -----------------------------
    public void calculateBalance ()
    {
        interestAmount = (accountBalance * yearlyInterestRate);
        accountBalance += interestAmount;
    }
// ----------------------------------------------------------------------------------

    public String toString()
    {
        return super.toString() + " Interest Received: " + interestAmount;
    }


}

Account class, if needed

import java.util.Random;
import java.text.NumberFormat;

public abstract class Account
{
    protected double accountBalance;
    protected long accountNumber;
    protected String accountHolder;
    public Account (String name)
    {
        accountHolder = name;
        accountBalance = 0;
        Random accountNo = new Random();
        accountNumber  = accountNo.nextInt(100000);
    }

    public double deposit (double amount)
    {
        accountBalance += amount;

        return accountBalance;
    }

    public String toString()
    {
        NumberFormat accountBal = NumberFormat.getCurrencyInstance();
        return "Account Balance: " + accountBal.format(accountBalance) + "
Account Number: " + accountNumber;
    }

    public String getAccountHolder()
    {
        return accountHolder;
    }

    public double getAccountBalance()
    {
        return accountBalance;
    }

    public abstract double withdraw (double amount);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();

This is because although you have an object of SavingsAccount you are using refrence variable of type Account so you can access only those methods that are there in Account class.

And you don't have calculateBalance() method in your Account class.

That's why you are not able to access it and compiler complains that it cannot find a method named calculateBalance as it sees that reference type is Account and there is no such method inside Account class.

If you want to use that method then change reference type to SavingsAccount :

SavingsAccount acct2 = new SavingsAccount (name);

Or you can explicitly cast it when accessing that method

((SavingsAccount) acct2).calculateBalance();

but be alert that it can throw ClassCastException if acct2 object is actually not an object of SavingsAccount

UPDATE:

But remember that at runtime, Java uses virtual method invocation to dynamically select the actual version of the method that will run, based on the actual instance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...