Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance in Java - "Cannot find symbol constructor"

I'm working on a class that inherits from another class, but I'm getting a compiler error saying "Cannot find symbol constructor Account()". Basically what I'm trying to do is make a class InvestmentAccount which extends from Account - Account is meant to hold a balance with methods for withdrawing/depositing money and InvestmentAccount is similar, but the balance is stored in shares with a share price determining how many shares are deposited or withdrawn given a particular amount of money. Here's the first few lines (around where the compiler pointed out the problem) of the subclass InvestmentAccount:

public class InvestmentAccount extends Account
{
    protected int sharePrice;
    protected int numShares;
    private Person customer;

    public InvestmentAccount(Person customer, int sharePrice)
    {
        this.customer = customer;
        sharePrice = sharePrice;
    }
    // etc...

The Person class is held in another file (Person.java). Now here's the first few lines of the superclass Account:

public class Account 
{
    private Person customer;
    protected int balanceInPence;

    public Account(Person customer)
    {
        this.customer = customer;
        balanceInPence = 0;
    }
    // etc...

Is there any reason why the compiler isn't just reading the symbol constructor for Account from the Account class? Or do I need to define a new constructor for Account within InvestmentAccount, which tells it to inherit everything?

Thanks

like image 255
benwad Avatar asked Feb 04 '09 10:02

benwad


People also ask

Can we use constructor in inheritance in java?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

What to do if Cannot find symbol in java?

Misspelled method name Misspelling an existing method, or any valid identifier, causes a cannot find symbol error. Java identifiers are case-sensitive, so any variation of an existing variable, method, class, interface, or package name will result in this error, as demonstrated in Fig.

What does error Cannot find symbol mean?

The “cannot find symbol” error comes up mainly when we try to use a variable that's not defined or declared in our program. When our code compiles, the compiler needs to verify all the identifiers we have. The error “cannot find symbol” means we're referring to something that the compiler doesn't know about.


2 Answers

use super(customer) in InvestmentAccounts constructor.

Java can not know how to call the only constructor Account has, because it's not an empty constructor. You can omit super() only if your base class has an empty constuctor.

Change

public InvestmentAccount(Person customer, int sharePrice)
{
        this.customer = customer;
        sharePrice = sharePrice;
}

to

public InvestmentAccount(Person customer, int sharePrice)
{
        super(customer);
        sharePrice = sharePrice;
}

that will work.

like image 56
Johannes Weiss Avatar answered Sep 29 '22 21:09

Johannes Weiss


You have to invoke the superclass constructor, otherwise Java won't know what constructor you are calling to build the superclass on the subclass.

public class InvestmentAccount extends Account {
    protected int sharePrice;
    protected int numShares;
    private Person customer;

    public InvestmentAccount(Person customer, int sharePrice) {
        super(customer);
        this.customer = customer;
        sharePrice = sharePrice;
    }
}
like image 42
Edison Gustavo Muenz Avatar answered Sep 29 '22 20:09

Edison Gustavo Muenz