+ 1

I tried running this code in Java Netbeans.I have a doubt as to why its volume of mybox1 and mybox2 is same??

class box {     double width,height,depth;     box(double w,double h,double d){         width=w;         height=h;         depth=d;     }     box(box b){         width = b.width;         height = b.height;         depth = b.depth;     }  /*   box(double len){         width= height= depth=len;     }*/     double volume(){         return width*height*depth;     } } class BoxWeight extends box{     double weight;     BoxWeight(double w,double h,double d,double m){         super(w,h,d);         weight = m;     }     BoxWeight(box b,double w){         super(b);         weight = w;     } } public class super_class {     public static void main(String args[]){         BoxWeight mybox1 = new BoxWeight(10.5 , 20.5 , 30.5 , 40.5);         BoxWeight mybox2 = new BoxWeight(mybox1, 5.5);         double vol;         vol = mybox1.volume();         System.out.println("Volume of mybox1 is "+ vol);         System.out.println("Weight of mybox1 is ");         System.out.println(mybox1.weight);         System.out.println("Volum

10th Nov 2021, 11:55 AM
Tanu Jain
7 Respuestas
+ 4
In this code here you have created three classes parent child and subclass ( box BoxWeight ,subclass) and u have used constructor. First u should understood about the constructor . Constructor is used to initialise the value if u won't define any default constructor it will created automatically when u creating object if u want to know more about this then create simple class and execute the code via command prompt then type this command javap name of file u will see default constructor. first compile the file by this command javac filename.java java filename.java javap filename Super keyword -- this is used when u have two class or more parent amd child and u have define same methods i child amd parent class then if you exteds these class and when u will try to access the method then then preference goes to local method means your child class method will be execute so to access method of parents class we can use super .
10th Nov 2021, 2:59 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Do you have knowledge of super and constructor chaining if yes then do dry run first
10th Nov 2021, 2:37 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Okay have u copied this code from any site it showing stray error when am compiling
10th Nov 2021, 2:46 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
I dont know what actul output u getting but am explaining how this code working
10th Nov 2021, 2:48 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
In main function u created object of BoxWeight class and u passing four parameters then so that your BoxWeight will be executed amd inside the body u have written super(w,h,d) so this represents your parent class it will execute the constructor of parent class which have three parameters means box class amd values will be initlize and m will be assigned to weight . Now in next line u have passed the BoxWeight object and 5.5 it will execute two parameterized constructor u can see in BoxWeight class same here again super(b) u passing value to parent class method and again values will be set and simply u printing values of mybox1.weight and mybox2.weight
10th Nov 2021, 3:03 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
No I typed in Netbeans
10th Nov 2021, 2:47 PM
Tanu Jain
0
No ♨️A.S.♨️ ,I have started this topic just now
10th Nov 2021, 2:43 PM
Tanu Jain