Why can you not inherit from a class whose constructor is private?

前端 未结 6 2080
粉色の甜心
粉色の甜心 2020-12-01 13:39

Why does Java disallow inheritance from a class whose constructor is private?

6条回答
  •  春和景丽
    2020-12-01 13:41

    This is because, When we do inheritance the job of the compiler is to make a direct or indirect relation of all the classes with the Object class by writing the super() at the very first statement of every class constructor . when we make the constructor as private that means it shouldn't be accessed from outside the class but when we do inheritance compiler will implicitly write this type of statement.

    class SubClassName extends SuperClassName {
      public SubClassName() {
        super();  // which will indirectly going to call the Parent class constructor from outside its scope
      }
    }
    

提交回复
热议问题