Incompatible return type error java

后端 未结 5 1520
礼貌的吻别
礼貌的吻别 2021-01-17 06:52
class BankAccount {
    private String firstname;
    private String lastname;
    private int ssn;
    private int accountnumber = 0;
    private double accountbala         


        
5条回答
  •  长发绾君心
    2021-01-17 07:12

    Every answer so far is correct and points out problems. Here is one that is not yet mentioned.

    Your equals method does not properly over-ride the parent:

    public boolean equals(BankAccount ba) {
        return this.accountbalance == ba.accountbalance;
    }
    

    The signature of equals on Object is public boolean equals(Object other) so that any objects can be compared to each other. You have changed it to only allow comparing against other BankAccounts, which violates the contract.

    Try this instead:

    public boolean equals(BankAccount ba) {
        if (ba instanceof BankAccount) {
            return this.accountbalance == ((BankAccount) ba).accountbalance;
        }
        else {
            return false;
        }
    }
    

提交回复
热议问题