Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending a class

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have class A
- here I have declared my constructor

I have class B. Class B extends class A
- here I have declared my constructor

Before Class B extended class A all compiled and worked fine for class A. And Class B (on there own).

But now I have extended Class A in Class B I get the following error message when compiling and I don't understand why?

C:\java\BankAccount>javac TestClass.java
.\Platinum.java:18: cannot resolve symbol
symbol : constructor Current ()
location: class Current
{
^
.\Platinum.java:26: cannot resolve symbol
symbol : constructor Current ()
location: class Current
{
^
2 errors

My understanding that when extending classes the constructors were not inherited?
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the visility modifier on that class's constructor "public"? Make sure it has the correct visibility given the location of the instantiating class.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also check you have defined a no-args constructor. The compiler only creates one if no constructor is defined.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To take one step back: every child class constructor has to call one of its superclass's constructors using the "super" keyword. If you don't do it explicitly, the Java compiler will insert a call to super() for you -- i.e., the superclass's no-argument constructor. If the superclass doesn't have one, then you'll get this compile error.

The fix may be to add an accessible no-argument constructor to the superclass, but if it didn't have one before, it probably shouldn't have one now. A better thing to do is to add an explicit call to a superclass constructor that does exist to each child class constructor.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have my Constuctors defined in the first Class current:


I then have my constructors in my second Class, which extends the first class Current:



To ensure my second Class doesn't use the Constructor for the Superclass i.e. the first Class I thought I'd be ok implementing this( ). Not the case though as I then get the following error:

C:\java\BankAccount>javac TestClass.java
.\Platinum.java:25: recursive constructor invocation
Platinum (String newAcctName, int newAccNo, double balance)
^
.\Platinum.java:16: recursive constructor invocation
Platinum (String newAcctName, int newAccNo)
^
2 errors

But without this ( ) I get the previous error?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ernst pointed out above, I think you want to use super(...) rather than this(...).

As the first statement in a constructor, super(...) calls the parent constructor that corresponds to the arguments provided. In contrast, this(...) calls an overloaded version of the constructor for the current class.

You're getting the error, "recursive constructor invocation" because the constructor is trying to call itself.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Worked like a dream - Thank you Marc and Ernst.
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maureen it is bad practice to store a bank balance in an estimated floating point arithmatic type e.g float or double. It is the norm to store the balance in the lowest unit of currency and then calculate from there. e.g in UK currency store the balance in pennies divide by 100 to get the amount in pounds modulus 100 to get the pence.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel Bowne, many thanks for your response.

I have taken your comments on board and used BigDecimal as I am implementing finances and don't want to lose any precision.

As this is a different subject I have posted it as another thread. (I don't know how to link this into here to pass people on so I am unable to point you there).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic