-4

The following program leads to an error message of the type "constructor A () not found in class A":

class A {
   A (int i) {}
} 
class B extends A {} 

If a constructor as defined below is in class B, however propose a legal program. Why?

class B extends A {
  B (int i) {
      super (i);
  }
}

1 Answers1

0

If you don't define a constructor, you get a default 0-ary constructor which does nothing except call the super constructor. So your first B class looks like

class B extends A {
  B() {
    super();
  }
}

And there's no A() for the super call to resolve to, hence the error. For this reason, the only time you can omit a constructor in a child class is if the parent can be default constructed.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116