When compiling this program, I get error-
class Person {
Person(int a) { }
}
class Employee extends Person {
Employee(int b) { }
}
public class A1{
public static void main(String[] args){ }
}
Error- Cannot find Constructor Person().
Why defining Person() is necessary?
When creating an
Employeeyou’re creating aPersonat the same time. To make sure thePersonis properly constructed, the compiler adds an implicit call tosuper()in theEmployeeconstructor:Since
Persondoes not have a no-argument constructor this fails.You solve it by either
adding an explicit call to super, like this:
or by adding a no-arg constructor to
Person:Usually a no-arg constructor is also implicitly added by the compiler. As Binyamin Sharet points out in the comments however, this is only the case if no constructor is specified at all. In your case, you have specified a Person constructor, thus no implicit constructor is created.