Can we override a constructor in Java and can a constructor be private?

后端 未结 11 1849
旧时难觅i
旧时难觅i 2020-12-29 14:21

I would appreciate an explanation for these questions:

  1. Can we Override a constructor in Java?
  2. Can a Constructor be private?<
11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 15:09

    1. In a derived class you can create a new constructor with the same signature but that is not really overriding since, when initializing the class, the parent class's constructor is still called before the new one.

    2. a class's constructor can be private or protected and of course be public. but if it is protected or private how would you initiate the class? ( actually you could with a static function in that class...)

提交回复
热议问题