Is it good to take input from user inside constructor !? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Is it good to take input from user inside constructor !?

26th Dec 2020, 10:49 AM
Abdul Wahab
Abdul Wahab - avatar
7 ответов
+ 2
Abdul Wahab you're welcome. And this is how super() works. https://code.sololearn.com/cuqhcU7gFv8f/?ref=app
26th Dec 2020, 11:40 AM
Avinesh
Avinesh - avatar
+ 2
I don't think if that is a practice that is encouraged but you can take all the user inputs inside the main method using local variables and then pass it to the constructor of an object that you want to instantiate.
26th Dec 2020, 11:06 AM
Avinesh
Avinesh - avatar
+ 1
You would have handled it in a different way if there is inheritance involved, in that case super() could be used. But for this case you could do something like this. Just an example. https://code.sololearn.com/czrzYD2ZKFQN/?ref=app
26th Dec 2020, 11:24 AM
Avinesh
Avinesh - avatar
+ 1
Ok then you can read on how to call super() from child class constructor to call parent class constructor. You can also pass arguments and the super() must be the first line inside the constructor. Do a little research and you will understand it better.
26th Dec 2020, 11:35 AM
Avinesh
Avinesh - avatar
+ 1
yeah sure i will watch videos. on utube . thanks man ♥️ really appreciated
26th Dec 2020, 11:37 AM
Abdul Wahab
Abdul Wahab - avatar
0
Avinesh nd wht about invoking other class constructor inside constructor for example Account(){ Person p = new Person()} }
26th Dec 2020, 11:09 AM
Abdul Wahab
Abdul Wahab - avatar
0
Avinesh yes inheritance is involed
26th Dec 2020, 11:33 AM
Abdul Wahab
Abdul Wahab - avatar