Can anyone tell me where the mistake as code is only printing zero | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone tell me where the mistake as code is only printing zero

It is the program of inheritance and many class are extended https://code.sololearn.com/cPaTCJT8qNRP/?ref=app

22nd Apr 2021, 8:35 AM
Prashant Pandey
Prashant Pandey - avatar
5 Answers
0
Your variables are not static, change them like this: static double len,width,height;
22nd Apr 2021, 10:54 AM
Abdulaziz Mavlonov
Abdulaziz Mavlonov - avatar
+ 3
You have to initialize the class variables you are using in rect etc. obj1 exists independent of robj etc. Read again about Classes and Objects in Java course, it looks like you have some misconceptions there You should use constructors to initialize variables.
22nd Apr 2021, 9:06 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
I believe your understanding on inheritance is incorrect. Inheritance is about inheriting parent's class property (methods and fields), so you did declare obj and robj, but they are independent of each other, so that means whatever value you put in obj will not be reflected in robj, to see the effect, you can replace your obj with this rect obj1 = new rect(); obj1.in(10.1,10.2,10.3); obj1.arect();
22nd Apr 2021, 8:58 AM
Hoh Shen Yien
Hoh Shen Yien - avatar
0
Thanks
22nd Apr 2021, 11:27 AM
Prashant Pandey
Prashant Pandey - avatar
0
Abdulaziz Mavlonov static is not the solution, although it might seem to be at first glance. What if you want to have two objects of type rect? We definitely have to use object variables here
22nd Apr 2021, 11:43 AM
Benjamin Jürgens
Benjamin Jürgens - avatar