According to the Java tutorial on constructors:
You don\'t have to provide any constructors for your class, but you must be careful when doing this.
And in the
main
method of a driver class you doA obj1 = new A();
a default constructor will be created
There is nothing you can do in the main
method of a driver to create a default constructor. If it is defined, you can use it; if it is not defined, you get a compile error.
So when will the superclass not have a no-argument constructor?
When it has other constructors, all of which taking some arguments. Here is an example:
class SuperA {
public SuperA(String str) { ... }
public SuperA(int num) { ... }
}
Above, SuperA
has two constructors - one taking a String
, and another one taking an int
. Neither of them is default, because they take parameters.
If you make a derived class DerivedB extends SuperA
and do not define any constructors, you would get a compile error.