Why we need more than one constructor in a class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we need more than one constructor in a class?

15th Oct 2019, 2:46 AM
Arshad
Arshad - avatar
4 Answers
+ 4
Because you may know different amounts of information about the objects you want to create at different times.
15th Oct 2019, 3:03 AM
Sonic
Sonic - avatar
+ 3
Please specify the language in Relevant Tags section, and remove people's names from there 👍
15th Oct 2019, 8:39 AM
Ipang
+ 2
Sometimes you only need one.
15th Oct 2019, 5:34 AM
Sonic
Sonic - avatar
+ 1
Not always, but sometimes it is a good practice. Let's create a class MyInt that stores only an int. It has two constructors, one which takes an int and the other takes nothing. If you want to create a MyInt object but want to set up the class later, you can use the constructor that takes nothing. Overall, having more than one constructor extends your class and can make it more flexible.
15th Oct 2019, 6:50 AM
Duck Typed
Duck Typed - avatar