views:

6134

answers:

5

Hi

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

+14  A: 

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.

Johannes Weiß
As I rule I always put the super() call in my constructor when applicable..
eljenso
+1  A: 

You have to call the contructor of the base class explicitly if it doesnt have a default constructor (one with no arguments). In your case, the contructor should be

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

And dont redefine customer as an instance variable of the subclass !

Guillaume
+1  A: 

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

Ritesh M Nayak
Not “should be” but “has to”.
Bombe
+1  A: 

Either define a default constructor in Account class like

public Account() {}

Or user super(customer) in class InvestmentAccount constructor.

Bhushan
+2  A: 

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

}

Edison Gustavo Muenz