Java constructor inheritance?

前端 未结 7 2026
一生所求
一生所求 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:15

    If superclass doesn’t have default constructor, then subclass also needs to have an explicit constructor defined. Else it will throw compile time exception. In the subclass constructor, call to superclass constructor is mandatory in this case and it should be the first statement in the subclass constructor.

提交回复
热议问题