Java constructor inheritance?

前端 未结 7 1970
一生所求
一生所求 2020-12-15 10:26

I always thought that constructors aren\'t inherited, but look at this code:

class Parent {
    Parent() {
        S         


        
7条回答
  •  时光说笑
    2020-12-15 11:17

    You write:

    It shows that Child inherited constructor.

    Constructors can not be inherited. Classes can be inherited, so Child does not inherit any constructor. Child inherits class Parent. Parent inherits class Object. When you call the Child constructor, automatically an Object constructor is called and then a Parent constructor, before the code of the Child constructor is run.

    This why you get this result:

    S1
    S2
    

提交回复
热议问题