I did a simple Inheritance program, but I got confused during the execution of this program.
Here is a simple program:
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1;
height = -1;
depth = -1;
}
double volume() {
return width * height * depth;
}
}
class BoxWeight extends Box {
double weight,h,w,d;
BoxWeight(double w, double h, double d, double m) {
this.h = h;
this.d = d;
this.w = w;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
}
}
What bothers me when I delete the constructor Box(), it gives me an error in the line where the constructor is defined BoxWeight(double w, double h, double d, double m).The error is: "constructor Box in class Box cannot be applied to given types".
If I call super()from BoxWeight(), then it works fine. I do not understand why? I know that the concept is pretty simple, but it's hard for me to understand. Any help appreciated.
source
share