Why do we need private constructors? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Why do we need private constructors?

Because we can't instantiate an object from these kind of classes. Could you explain when do we need it?

7th Feb 2018, 9:33 PM
Yusuf
Yusuf - avatar
5 Answers
+ 3
makes your code more secure when you use private
7th Feb 2018, 9:35 PM
Tim Millar
Tim Millar - avatar
+ 3
Now, I understand my mistake. We can use it in another class like this(I am trying to establish a relationship between inheritance and private constructor, that is why I am confused): class one{ string take; one(string pass) { take=pass } }; class two{ one obj("hey"); };
7th Feb 2018, 9:40 PM
Yusuf
Yusuf - avatar
+ 3
I confuse when I see the function return types as class. 😟 I need to learn this kind of functions. Where could I learn it? @Vukan
7th Feb 2018, 10:30 PM
Yusuf
Yusuf - avatar
0
I think private constructors are useful when the program needs a concept called self referenciation i.e.,the object of a class is created within the same class. So that the objects cannot be created in other classes. And according to the requirement of program. Am I right? 🤔
8th Feb 2018, 11:03 AM
Nashat
Nashat - avatar