• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

extending a class

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also check you have defined a no-args constructor. The compiler only creates one if no constructor is defined.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Worked like a dream - Thank you Marc and Ernst.
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic