The superclass does not need an empty constructor. A subclass just needs to call the constructor in the superclass. If the superclass has an open or protected no-arg constructor, this is called automatically, otherwise you need to be explicit.
Default constructor
public class Super { } public class Sub extends Super { }
Here Super does not specify a constructor, so it is added. Same thing for Sub . The above really looks like this:
public class Super { public Super() { } } public class Sub extends Super { public Sub() { super(); } }
Explicit no-arg constructor
public class Super { public Super() { } } public class Sub extends Super { }
It is legal. The constructor is added to Sub , which calls the default constructor Super .
Explicit constructor with arguments
public class Super { public Super(int i) { } } public class Sub extends Super { }
This is a compilation error like yours. Since Super has a constructor, the no-arg constructor is not automatically added by the compiler. A way to handle this:
public class Super { public Super(int i) { } } public class Sub extends Super { public Sub() { super(0);
source share