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

后端 未结 11 1848
旧时难觅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:02

    You can not override a constructor, however you can make them any access level modifier such as public, private or default. You would want a private constructor for things like a singleton or for a class that's mostly made of static methods and such (i.e Java's Math class)

提交回复
热议问题