Trafton
Trafton

Reputation: 153

Java - Inheritance & Constructors Error

I am getting an error when I compile my code due to an issue with my constructors.

Here is my parent class constructor:

   public BankAccount(final String theNameOfOwner, final double theInterestRate)
   {
      myName = theNameOfOwner;
      myInterestRate = theInterestRate;
      myBalance = 0;
      myMonthlyWithdrawCount = 0;
      myMonthlyServiceCharges = 0;
   }

Here is my child class constructor:

   public SavingsAccount(final String theNameOfOwner, final double theInterestRate)
   {
      BankAccount(theNameOfOwner, theInterestRate);
      myStatusIsActive = false;
      myWithdrawalCounter = 0;
   }

I am getting the following error:

SavingsAccount.java:7: error: constructor BankAccount in class BankAccount cannot be applied to given types;
   {
   ^
  required: String,double
  found: no arguments
  reason: actual and formal argument lists differ in length

The error says that I require String,double parameters in my BankAccount call in my child constructor if i'm understanding this correctly. The only problem is it looks like I have those parameters correct. Any help/input would be greatly appreciated since I just started programming Java! Thank you!

Upvotes: 0

Views: 59

Answers (2)

rgettman
rgettman

Reputation: 178343

That is not the way to call the superclass constructor. The compiler thinks you're trying to call a method called BankAccount which doesn't exist. Because there is no explicit call to a superclass constructor, it attempts to insert the implicit call to the default superclass constructor, and that doesn't exist either, leading to the compiler error you see.

Use the super keyword to call a superclass constructor. Change

BankAccount(theNameOfOwner, theInterestRate);

to

super(theNameOfOwner, theInterestRate);

Upvotes: 3

Chad
Chad

Reputation: 1980

I think what you need on the line causing the error is the following instead:

super(theNameOfOwner, theInterestRate);

Upvotes: 1

Related Questions