Search code examples
javaconstructoroverriding

Is Constructor Overriding Possible?


What I know is: the compiler writes a default no-argument constructor in the byte code. But if we write it ourselves, that constructor is called automatically. Is this phenomenon constructor overriding?


Solution

  • What you describe isn't overriding. If you don't specify a default constructor, the compiler will create a default constructor. If it's a subclass, it will call the default parent constructor(super()), it will also initialize all instance variables to a default value determined by the type's default value(0 for numeric types, false for booleans, or null for objects).

    Overriding happens when a subclass has the same name, number/type of parameters, and the same return type as an instance method of the superclass. In this case, the subclass will override the superclass's method. Information on overriding here.