class A {
A(int i){
System.out.println("A(int)");
}
}
class B1 extends A{
public static void main(String args[]){
A ob=new A(2);
}
}
class A { A(int i){ System.out.println(A(int)); } } class B1 extends A{ public static
Share
You have declared a 1-arg
constructorinclass A. So, compiler does not provide a default 0-argconstructor.Now, in
class B1, you have not defined anyconstructor, so compiler adds a default 0-argconstructorin that class, which looks like this: –As you can see, Compiler adds a
super()call to invoke the0-arg constructorof thesuper class, which isclass A, in this case.Now, since your
class A, does not have any 0-argconstructor, hence the error.So, either you can add a
0-arg constructorin your class A: –This will solve the issue.
Or, add a
0-arg constructorin yourclass B1explicitly, and add asuper()call to the1-arg constructorofclass A: –But, the problem in the 2nd solution will be that, from every constructor in your
class B1, you would have to invoke the1-arg constructorofclass Aexplicitly. As soon as you miss one, you will get acompilererror immediately.So, I would suggest to go with the
1st option. Add a0-arg constructorinclass A. And you are all good.