This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Overloaded Constructor Not Working - Cannot Find Symbol Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloaded Constructor Not Working - Cannot Find Symbol" Watch "Overloaded Constructor Not Working - Cannot Find Symbol" New topic
Author

Overloaded Constructor Not Working - Cannot Find Symbol

Ben Stanley
Greenhorn

Joined: Sep 01, 2010
Posts: 15
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!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Constructors are not inherited. You'll need to respecify them, then simply call the super constructor:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ben Stanley
Greenhorn

Joined: Sep 01, 2010
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19679
    
  18

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

Joined: Sep 01, 2010
Posts: 15
Good to know, thanks!
 
 
subject: Overloaded Constructor Not Working - Cannot Find Symbol