10

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

5 Answers 5

25

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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;
    }
}

Comments

1

Call the super() method. If you want to call the Account(Person) constructor, use the statement super(customer); Also this should be the first statment in your InvestmentAccount constructor

1 Comment

Not “should be” but “has to”.
1

You have to call the constructor of the base class explicitly if the base class doesn't have a default constructor (one with no arguments).

In your case, the constructor should be:

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

And don't redefine customer as an instance variable of the subclass!

Comments

1

Either define a default constructor in the Account class:

public Account() {}

Or call super(customer) in the InvestmentAccount constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.