#Call to the Superclass's Constructor should be the first line

1 messages · Page 1 of 1 (latest)

tacit arrow
#

If the superclass doesn’t have a default constructor, then the subclass also needs to have an explicit constructor defined. Otherwise, it will throw a compile-time exception. In the subclass constructor, a call to the superclass constructor is mandatory in this case, and IT SHOULD BE THE FIRST LINE IN THE SUBCLASS CONSTRUCTOR.

Does the highlighted part still hold in JDK 25?
I checked multiple online resources and verified it with AI. According to both, the statement above is indeed true.

But I encountered a contradiction, as the following part, even after having super as the second line, runs fine.
public Cat(boolean veg, String food, int legs, String color){
this.color=color;
super(veg, food, legs);
}

grizzled estuaryBOT
#

<@&987246399047479336> please have a look, thanks.

thorn bone
#

the rules were relaxed

#

it doesnt have to be the first line anymore

tacit arrow
#

Thanks mate

empty drum
grizzled estuaryBOT
#

@tacit arrow

Your question has been closed due to inactivity.

If it was not resolved yet, click the button below to keep it
open, or feel free to create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍