cannot find symbol symbol : constructor A() location: class A

class, constructor, inheritance, java, object

Solution

You have declared a 1-arg `constructor` in `class A`. So, compiler does not provide a default 0-arg `constructor`.

Now, in `class B1`, you have not defined any `constructor`, so compiler adds a default 0-arg `constructor` in that class, which looks like this: -

public B1() {
    super();
}

As you can see, Compiler adds a `super()` call to invoke the `0-arg constructor` of the `super class`, which is `class A`, in this case.

Now, since your `class A`, does not have any 0-arg `constructor`, hence the error.

So, either you can add a `0-arg constructor` in your class A: -

public A() {

}

This will solve the issue.

Or, add a `0-arg constructor` in your `class B1` explicitly, and add a `super()` call to the `1-arg constructor` of `class A`: -

public B1() {
    super(0);  // Or any value
}

But, the problem in the 2nd solution will be that, from every constructor in your `class B1`, you would have to invoke the `1-arg constructor` of `class A` explicitly. As soon as you miss one, you will get a `compiler` error immediately.

So, I would suggest to go with the `1st option`. Add a `0-arg constructor` in `class A`. And you are all good.

Problem

``` 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); } } ```

Original source