Reputation: 837
I was doing a simple Inheritance program, However I got confused while doing this program.
Here is the simple program:
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
//Error on removing the Box() Constructor
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) {
// If i add super(w,h,d) here, then it is working, WHy?
this.h = h;
this.d = d;
this.w = w;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
//Implementation of above
}
}
What confused me is, When i remove the Box()
constructor, It gives an error to me on line where BoxWeight(double w, double h, double d, double m)
constructor is defined. The error is: "constructor Box in class Box cannot be applied to given types".
If i call super()
from within the BoxWeight()
, then it works fine. I don't understand why?? I know the concept is pretty simple, but I am having a hard time understanding it. Any Help is Appreciated.
Upvotes: 2
Views: 189
Reputation:
What is actually happening in your program when you remove the default constructor Box
then the compiler has no constructor to invoke from within the BoxWeight
constructor. This creates a confusion for the compiler and hence it gives you an
error.
But however, when you supply a call to the constructor of Box
that accepts parameters, your code compiles without any errors.
Upvotes: 1
Reputation: 61986
When a class does not define its own constructor, then a default implicit parameterless constructor is automatically provided by the language.
When a class defines at least one constructor, then no implicit constructor is provided by the language, so the class may only be instantiated using one of the constructors that it defines.
When the constructor of a derived class does not invoke any constructor of its super class, then the language automatically supplies an implicit call to super()
. But for this to work, the super class must have an (explicit or implicit) parameterless constructor.
So, what is happening with the above code is that the constructor of BoxWeight
is not explicitly invoking super()
, so an implicit call to super()
is provided by the language. This works, because Box
defines an explicit parameterless constructor Box()
.
When you remove the explicit parameterless constructor Box()
then the compiler has no constructor of Box
to invoke from within the BoxWeight
constructor. It cannot invoke the remaining constructor, because it takes parameters, and the compiler cannot just decide for you what parameters to pass. And as you have discovered by yourself, if you supply a call to the constructor of Box
which accepts parameters, your code compiles again.
Also, please note for future reference: "It gives an error to me" is not an adequate description of a problem. It gives you a very specific error, which you must include in your question. The error is: "constructor Box in class Box cannot be applied to given types".
Upvotes: 2
Reputation: 994
When you are creating objects of a sub class, if you don't explicitly make a call to the constructor of the immediate super class of the class, then implicitly, the default constructor of the immediate super class is called (and this chain continues up the inheritance hierarchy).
This is what you are doing with the call to super(w, h, d)
. You are explicitly making a call to the constructor of the super class of Box. In this case, the implicit call to super()
is omitted. super()
is a call to the default constructor of the super class. But since you have not included a default constructor, it results in an error.
However, say you add a default constructor to your Box class. Then also this code should work fine, since now, a call to super()
invokes the default constructor of the super class
Upvotes: 2
Reputation: 7496
Your constructor BoxWeight(double w, double h, double d, double m)
does not call a super constructor explicitly. This means that the default constructor Box()
is called.
If you remove that constructor or make it inaccessible from BoxWeight
you will get this error.
What you can do instead is calling the other constructor explicitly, as you already found out:
BoxWeight(double w, double h, double d, double m) {
super(w,h,d);
this.h = h;
this.d = d;
this.w = w;
weight = m;
}
An inherited class can only be constructed by calling a constructor on the super class.
Upvotes: 4