Use of Private Constructor in OOPS
If we set access specifier of a constructor to private, then that constructor can only be accessed inside the class. A private constructor is mainly used when you want to prevent the class instance from being created outside the class. This is mainly in the case of singleton class. Singleton classes are employed extensively in concepts like Networking and Database Connectivity. Using private constructor we can ensure that no more than one object can be created at a time. Example of Private Constructor in a Singleton Class public class SingletonClass { public static SingletonClass singletonClass; private SingletonClass() { } public static SingletonClass getInstance() { if(singletonClass == null) { singletonClass = new SingletonClass(); } return singletonClass; } } A class with private constructor cannot be inherited. If we don’t want a class to be inherited, then we make the class constructor private. So, if we