Juliet C. answered 03/29/21
Multiple semesters of teaching Java programming courses
In Java, when no constructor is defined in a class, the default no-argument constructor is generated. When a class is a subclass, the generated default constructor involves calling the no-argument constructor of the superclass. In this case, however, the superclass (Anything) does not have a no-argument constructor (a.k.a. "Implicit constructor Anything()") because it only defines a constructor that takes an argument. Therefore, Java cannot generate the default no-argument constructor for the SpecificThing class.
One way to fix the code is to define a no-argument constructor in either the Anything class or the SpecificThing class. For instance, in the Anything class, add the following code:
In the SpecificThing class, the constructor would need to call the superclass constructor that takes an argument, e.g.:
Another way to fix the code is to define a constructor in the SpecificThing class that takes an argument and calls the superclass constructor, and then change the code in main to use that constructor instead, as shown below.
In the SpecificThing class, add the following code:
In the main method in Main.java, change the first line to be: