Can't get the output while using Constructor method. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can't get the output while using Constructor method.

An output error is: color(attribute) has private access in Vehicle(class) ! https://code.sololearn.com/crh6nPW406l4/?ref=app

8th Aug 2021, 2:03 PM
M Zulqarnain Khan
M Zulqarnain Khan - avatar
4 Answers
0
Zulqarnain Khan You cannot access private members directly using class object. Make setter getter then access value of color. That's called incapsulation.
8th Aug 2021, 2:07 PM
A͢J
A͢J - avatar
+ 1
Now let me clear that, ... Constractors are used for initialization with already created objects, and can also be used to provide initial value for objects attribute(variable). Thanks in advance! :)
9th Aug 2021, 10:29 AM
M Zulqarnain Khan
M Zulqarnain Khan - avatar
0
Then what is the main purpose of Constructors at all !
8th Aug 2021, 2:11 PM
M Zulqarnain Khan
M Zulqarnain Khan - avatar
0
Zulqarnain Khan Constructor is used to initialise value and object not to access class members. As you have created parameterized constructor and passed value so it will change the original value of color. But if you want to access class members directly using class objects then make public.
8th Aug 2021, 2:17 PM
A͢J
A͢J - avatar