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

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

    Constructor is meant for a class. It cant be overridden under any circumstances. Its like wanting to change Ferrari's factory from BMW's factory (which isn't practical). Surely you can overload to get the functionality you need.

    Yes Constructor can be private. By making it private you are not letting the outside world to create an object of it directly through constructor, But singleton pattern uses a public static method to call the constructor of the class and object can be created.

提交回复
热议问题