• 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

Overloaded Constructor Not Working - Cannot Find Symbol

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks. I wanted to make sure I understood the basics of constructor overloading, how subclasses inherit the constructors of their superclasses, etc. To that end, I created the following three files (note that these aren't for a real program):

(1) An abstract class called AlienHostile that holds one instance variable (int hitpoints). The class contains two constructors, one no argument constructor that sets hitpoints to 10 by default, and an overloadded constructor that sets hitpoints to an int argument that is passed to the constructor.



(2) A subclass of AlienHostile that has only a single instance variable (boolean recon).


(3) A test class to see how everything worked. The goal was to create a new AlienFighter object and pass it an int argument (in this case, 23) and then see that the overloaded constructor had in fact been called by calling the getHitpoints accessor method on the object and printing its output.


However, once I compile everything and attempt to run the test class, I get the following error:

(If it's not obvious from the code above, the arrow is pointing towards the word "new")

If I remove the int argument and the no argument constructor is called, everything works properly.

Any insights would be much appreciated. Thanks!

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors are not inherited. You'll need to respecify them, then simply call the super constructor:
 
Ben Stanley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

Thanks! That worked!

I want to make sure I'm understanding what you just you did: a constructor isn't inherited because it's not a variable or method that an object possesses; rather, it's the thing that creates an object. But since an instance of a subclass also "contains" an instance of its superclass(es), the constructor for the superclass is called when you instantiate a subclass object. To invoke an overloaded constructor of a superclass, all you have to do is pass an appropriate argument to the superclass constructor by using super() in the subclasses constructor.

Thanks again,

Ben
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. And if you don't start your constructor with a call to super(...) or this(...)* then the compiler will put a call to super() in it for you.

* this(...) will call another constructor of the same class, like super(...) calls a constructor of the super class.
 
Ben Stanley
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to know, thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic