Can an abstract class have a constructor?

前端 未结 22 2214
甜味超标
甜味超标 2020-11-22 05:25

Can an abstract class have a constructor?

If so, how can it be used and for what purposes?

22条回答
  •  误落风尘
    2020-11-22 06:10

    Since an abstract class can have variables of all access modifiers, they have to be initialized to default values, so constructor is necessary. As you instantiate the child class, a constructor of an abstract class is invoked and variables are initialized.

    On the contrary, an interface does contain only constant variables means they are already initialized. So interface doesn't need a constructor.

提交回复
热议问题