Is Constructor Overriding Possible?

后端 未结 14 1877
时光说笑
时光说笑 2020-12-08 00:31

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 pheno

相关标签:
14条回答
  • 2020-12-08 01:34

    Your example is not an override. Overrides technically occur in a subclass, but in this example the contructor method is replaced in the original class.

    0 讨论(0)
  • 2020-12-08 01:36

    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.

    0 讨论(0)
提交回复
热议问题