Java — private constructor vs final and more

前端 未结 3 774
轮回少年
轮回少年 2020-12-10 16:11

Suppose there is a class with all of its constructors declared as private.

Eg.:

public class This {
    private This () { }

    public someMethod(          


        
3条回答
  •  暖寄归人
    2020-12-10 16:20

    A class with private constructors cannot be instantiated except form inside that same class. This make it useless (possible, but will not compile) to extend it from antoher class.

    This does not mean it cannot be subclassed at all, for example among inner classes you can extend and call the private constructor.

提交回复
热议问题