in my program, i have three programs that inherit the properties of a superclass "Inventory Object," but for some reason, the constructors of these subclasses won't run because of the following error: "Implicit super constructor InventoryObject() is undefined. Must explicitly invoke another constructor.
The declaration for the subclass constructor: public ReceivedInventory(String vendor, double itemCost...etc etc) { code }
The declaration for the super constructor: public InventoryObject(String vendor, double itemCost...etc etc) { code }
The difference between the two constructors is that the subclass constructor has all the superclass constructors + receipt date, days aged, etc etc. Can anyone help me?
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Constructors are tricky if you don't understand the intricate but deterministic flow design.
First rule: the first statement of a constructor is ALWAYS a call to another constructor - either another constructor in the same class or a call to a constructor in the immediate superclass. This is done explicitly using this() and super().
Second rule: if you DID NOT explicitly call another constructor in the first line of your constructor, the compiler will insert "super();" into the compiled code (not your source), in order to make it compliant with rule 1. This is a convenience to you so that you don't have to bother with a "super();" line if you just want it to call the default/no arguments constructor of the superclass.
Why rule 1 and 2? This guarantees all inherited instance variables are initialized in a top-down fashion, from java.lang.Object first, all the way back down to your latest subclass. Very consistent, deterministic, and safe! It helps keep you from incorrectly initializing data through a subclass, and helps ensure the final state is set by the latest subclass to what you expect. No surprises or weird outcomes!
So, in your particular case, it is obvious that the constructors in your subclasses don't use super() or this() to call a named constructor and thus the compiler is inserting "super();". This is a call to the "no argument" default constructor in InventoryObject. But, InventoryObject must not have a default constructor! Therefore, to fix this, your subclass constructors should have a first line that passes the appropriate arguments on up to the InventoryObject constructor to initialize.
Example: assume InventoryObject has a constructor with 2 arguments - one for the vendor and one for the cost.
public InventoryObject(String vendor, double itemCost){
super();//optional: compiler would insert anyway if left out
setVendor(vendor);//maybe this method also validates it...
setCost(itemCost);//maybe this verifies >0
}
..............................................
//ReceivedInventory constructor
public RecievedInventory(String vendor, double itemCost, Date rcvd){
//call InventoryObject constructor explicitly
super(vendor, itemCost);//initialization of vendor and cost done in super class
setReceivedDate(rcvd);//maybe validates date not in future
}
Java Super Constructor