This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes extending a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending a class" Watch "extending a class" New topic
Author

extending a class

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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

Joined: May 29, 2003
Posts: 1258
Is the visility modifier on that class's constructor "public"? Make sure it has the correct visibility given the location of the instantiating class.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Also check you have defined a no-args constructor. The compiler only creates one if no constructor is defined.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218

Worked like a dream - Thank you Marc and Ernst.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
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

Joined: Oct 04, 2004
Posts: 218
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: extending a class