Looking at the documentation for Constructor it says
When you create a Constructor for any subclass, the Code Editor automatically inserts code that calls the Constructor for its super class using the Super keyword. If there is more than one Constructor, it inserts calls to all of them.
I created Class A with multiple constructors and then subclass Class B and added a constructor. The code editor didn’t add anything to the subclass constructor method. I figured out what I needed to do with super.constructor but should something have been added automatically?
This calls the overloaded superclass constructor. If the superclass should never be constructed directly then make its constructor method protected. (Protected means it can only be called from a subclass.)
I think I’ve kind of got a handle on what’s going on but kind of surprised at how some of this works.
Given base_class with: Constructor(x as integer, y as integer)
and sub_class with: Constructor(x as integer, y as integer, z as integer)
these all work:
var basec as new base_class(1, 2)
var subc1 as new sub_class(1, 2, 3)
var subc2 as new sub_class(1, 2)
I wasn’t expecting that last line to work, because the sub class doesn’t have a constructor method signature to match. But I guess it’s just searching the next level up to find something that matches?
For my current purposes, having instances of both the base class and sub class would be valid. But I also want enforce that all instances of the sub class are only created with the sub class constructor signature. Ie. using (x, y, z) rather than inadvertently using (x, y).
So I set the base class constructor to protected and added a subclass that just copies the base class constructor. This gives me:
var subc1 as new sub_class(1, 2, 3)
var copy1 as new copy_of_base_class(1, 2)
and it works. But is it the best (or only) way of handling that scenario?