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

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

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

6条回答
  •  广开言路
    2020-12-01 13:50

    If you have a subclass, you have 2 possiblities for child class(subclass) constructors : 1. Default Constructor(No argument constructor) : In this case default constructor will automatically try to call the parent class constructor : this will fail since parent class constructor is private. 2. Parameterized Constructor : When you try to create an object for a child class which has parameterized constructor, you need to mandatorily call parent class constructor from child class constructor by either passing parameters or not passing parameters : this will also fail since parent constructor is private.

    Since child class will have either default constructor or parameterized constructor and its not possible to have either of them, you cannot have a subclass for a parent class with private constructor.

提交回复
热议问题