How to prevent object creation for a class?
i. Make that class as abstract class.
ii. Make all the constructors in that class as private constructors.
iii. Make that class as private outer class.
i & ii
i, ii, & iii
You can achieve this in 2 ways.
1. Make that class as abstract class.
Note: As you already know that no one can create object for an abstract class.
2. Make all the constructors in that class as private constructors. Since constructor is not accessible to outside world, no body can create object for that class from outside.
Note: Remember in outer level we can't give a class as private. We can give only public or default.
Back To Top