This is how I encountered the problem. I am giving an example:
package check;
public class Check {
int a,b;
Check (int i,int j) {
a = i;
b = j;
Reason for the error-
Child class constructor calls the constructor of the parent which takes no parameters.
There is no constructor in the
Check
class which takes no parameters.
Furthermore,
Java compiler automatically insert super()
constructor to the child class. Meaning of this super constructor is Go and execdute a constructor in the parent class which takes no parameters.
According to your program:
Check (int i,int j) {
a = i;
b = j;
}
When you run your program compiler remove the default constructor because constructor in parent class takes two parameters.
If you do not add constructor for your V
class Java compiler automatically insert default constructor. As I said before when you extends
another class(Check
) in the default constructor also Java compiler automatically insert the super()
in the first line of default constructor.
It's look like this:
class V extends Check {
V(){super();}
}
We can add super()
to child class's constructor in a way that it matches an existing constructor in the parent class.
In your second question also super()
insert automatically. there is no matching constructor for this in parent class because of that Compilation Error will occur.
Note: when you add super()
must be the first line in child class constructor.