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.
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.