user3362681
user3362681

Reputation: 23

Unknown error preventing construction of subclasses

Here is my superclass:

public abstract class BankAccount
{
    private String name;
    private double balance;
    final int OVERDRAFT_FEE = 35;
    private static int accountNum=0;
    public BankAccount(String name)
    {
        this.name=name;
        balance=0;
        accountNum++;
    }

    //various methods omitted

    public abstract void endOfMonth();
    public String toString(BankAccount account)
    {
        return "Account Name: "+name+"\nBalance: $"+balance+"\nAccount Number: "+accountNum;
    }
}

and here is my subclass:

public class CheckingAccount extends BankAccount
{
    final int TRANSACTION_LIMIT = 3;
    final int MINIMUM_BALANCE = 50;
    final int FEE = 10;
    private int transactionCount;

    // various methods omitted

    public void endOfMonth()
    {
        if(transactionCount>TRANSACTION_LIMIT)
        {
            int extra = transactionCount-TRANSACTION_LIMIT;
            int extra_fee = FEE*extra;
            super.setBalance((super.getBalance)-extra_fee);
        }
        if(super.getBalance()<MINIMUM_BALANCE)
        {
            super.setBalance((super.getBalance)-FEE);
        }
    }
}

it gives me an exception when compiling the subclass saying "constructor BankAccount in class BankAccount cannot be applied to given types; required: java.lang.String; found: no arguments; reason: actual and formal argument lists differ in length"

i thought it was a problem with the abstract method at first but getting rid of it didn't fix anything. thoughts?

Upvotes: 2

Views: 108

Answers (4)

Jigar Joshi
Jigar Joshi

Reputation: 240898

because super class's default constructor is not present, add a default constructor in BankAccount that is being called by default or call the right version of constructor for super class

Upvotes: 1

Ashalynd
Ashalynd

Reputation: 12563

Your child class, by default, will try to call the default constructor of its parent class, but you haven't provided it. There are two ways to fix this:

1) add default constructor to the parent class ( public BankAccount() without arguments)

2) call the parent constructor from the child class explicitly:

public ChildAccount() {
    super("Checking Account");
}

(See a similar example here: http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ308_002.htm)

Upvotes: 0

user800014
user800014

Reputation:

In Java when you declare a constructor in the abstract class that have arguments, all subclasses need to declare this constructor. You can add the constructor to CheckingAccount:

class CheckingAccount extends BankAccount {
  public CheckingAccount(String name){
    super(name) //calling the BankAccount constructor.
  }
}

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279990

Your parent class constructor is declared as

public BankAccount(String name)

In other words, it expects a String argument.

By not explicitly providing a constructor for your CheckingAccount class, the Java compiler creates one that looks like

public CheckingAccount() {
    super();
}

In other words, it doesn't provide any arguments to your parent constructor.

You need to change that. Declare a constructor with an appropriate parameter and pass it on to the parent.

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

Upvotes: 1

Related Questions