Below is the code snippet and its output -
class Box{
int height = 1;
int length = 1;
int width = 1;
Box(int h, int l, int w){
this.height = h;
this.length = l;
this.width = w;
}
int volume(){
return this.height * this.length * this.width;
}
}
class BoxWeight extends Box{
int height = 99;
int length = 99;
int width = 99;
int mass;
BoxWeight(int h, int l, int w, int m) {
super(h, l, w);
this.mass = m;
}
int volume(){
return this.height * this.length * this.width;
}
}
public class BoxDemo {
public static void main(String[] args) {
Box b = new Box(10, 20, 30);
BoxWeight bw = new BoxWeight(40, 50, 60, 10);
System.out.println(bw.height);
System.out.println(bw.length);
System.out.println(bw.width);
//b = bw;
//System.out.println(b.volume());
}
}
Output
99
99
99
Here i am not able to get why object bw is printing values initialized as class members. Why object bw is not holding values assigned through constructor?