What is Implicit constructors on Java

前端 未结 2 2031
囚心锁ツ
囚心锁ツ 2020-12-06 18:16

Is it mandatory to call base class constructor in Java? In C++ it was optional, so I am asking this.

When I extend ArrayAdapter, I get this error:

相关标签:
2条回答
  • 2020-12-06 18:29

    It's not necessary to call the no-args constructor of super class. If you want to call a constructor with args, user super keyword like show below:

    super(arg1, ...);
    
    0 讨论(0)
  • 2020-12-06 18:39

    The no-args constructor is called implicitly if you don't call one yourself, which is invalid if that constructor doesn't exist. The reason it is required to call a super constructor is that the superclass usually has some state it expects to be in after being constructed, which may include private variables that can't be set in a sub-class. If you don't call the constructor, it would leave the object in a probably invalid state, which can cause all kinds of problems.

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