Private constructor in abstract class

后端 未结 7 1134
无人及你
无人及你 2020-12-31 05:54

In Java what is the purpose of using private constructor in an abstract class?

In a review I got this question, and I am curious, for what situation we need to use

7条回答
  •  Happy的楠姐
    2020-12-31 06:46

    A private constructor in an abstract class can also serve the purpose of sealed classes (like in Scala or Kotlin etc.). Since you can still provide subclasses from within the abstract class, but outsiders cannot extend/implement (as @Marko Topolnik answered).

    It does look like we will be getting sealed interface to more cleanly support this. See https://openjdk.java.net/jeps/8222777

提交回复
热议问题