Is it possible to apply inheritance to a Singleton class?

前端 未结 8 1990
孤独总比滥情好
孤独总比滥情好 2021-01-30 23:05

Today I faced one question in interview. Is it possible to apply inheritance concept on Singleton Classes? I said since the constructor is private, we cannot extend that Singlet

8条回答
  •  执念已碎
    2021-01-30 23:40

    You could make the constructor package-private. This way, no classes outside the package could instantiate the Singleton class but derived classes could call the upper class constructor.

    But, as others said, this is really not advisable.

    Stupid interview question by the way.

提交回复
热议问题