0

Why would the following code yield a compilation error (The constructor AA() is undefined)? Shouldn't it call a default constructor?

public class A{
    public A(){ }
}

public class AA extends A{
    public AA(int aa){ }
}

public class C{
    public static void main(String[] args){
        A a= new AA();
    }
}
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Jaja
  • 105
  • 5

2 Answers2

2

The compiler will only add a default constructor to a class if there is no constructor in the class which is not the case for AA.

8.8.9 Default Constructors (Java language specification)

If a class contains no constructor declarations, then a default constructor is implicitly declared

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
0

You write

A a= new AA(); // try to invoke default constructor

But there is no default constructor in class AA because you write your own constructor

public AA(int aa){ }

So try this:

int someInteger = 1;
A a= new AA(someInteger);
Community
  • 1
  • 1
mazhar islam
  • 5,561
  • 3
  • 20
  • 41