The following code gives a compilation error:
class parent {
parent(int a){}
}
class child extends parent{}
Error:
Main.java:6: cannot find symbol
symbol : constructor parent()
location: class parent
class child extends parent{}
^
1 error
I was trying to do different things and found that adding a return type to the parent constructor got rid of the error!!!
class parent {
int parent(int a){}
}
class child extends parent{}
I've read that constructors should not have return type, which clearly is not correct all the times. So my question is when should we have return type for constructor?
In case 1 the child class does not have any constructor so the compiler adds a default constructor for you and also adds a call to superclass constructor. So your child class effectively looks like:
class child extends parent {
child() {
super();
}
}
the call super()
looks for a zero argument constructor in the base class, since there is no such constructor you get the error.
In case 2 parent parent class does not have any constructor
int parent(int x) {}
is not a constructor as it has a return type. Its just a method which has the class name. The child class does not have any constructor as well. So the compiler adds default constructor for both child and parent and also adds call to super class constructor:
class parent {
parent() {
super(); // calls Object class ctor.
}
int parent(int x) {} // not a ctor.
}
class child extends parent {
child() {
super();
}
}
Constructor must not have a return type. By definition, if a method has a return type, it's not a constructor.
JLS 8.8 Constructor Declarations
A constructor is used in the creation of an object that is an instance of a class. [The name must match the class name, but], in all other respects, the constructor declaration looks just like a method declaration that has no result type.
The following snippet does give a compilation error:
class Parent {
Parent(int a){}
}
class Child extends Parent{
// DOES NOT COMPILE!!
// Implicit super constructor parent() is undefined for default constructor.
// Must define an explicit constructor
}
The reason is not because of a return type in a constructor, but because since you did not provide ANY constructor for Child
, a default constructor is automatically created for you by the compiler. However, this default constructor tries to invoke the default constructor of the superclass Parent
, which does NOT have a default constructor. THAT'S the source fo the compilation error.
Here's the specification for the default constructor:
JLS 8.8.9 Default Constructor
If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:
- If the class being declared is the primordial class
Object
, then the default constructor has an empty body.- Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
The following is a simple fix:
class Parent {
Parent(int a){}
}
class Child extends Parent{
// compiles fine!
Child() {
super(42);
}
}
The following snippet DOES compile:
// COMPILES FINE!!
class Parent {
// method has same name as class, but not a constructor
int Parent(int a) {
System.out.println("Yipppee!!!");
return 42;
}
// no explicit constructor, so default constructor is provided
}
class Child extends Parent {
// no explicit constructor, so default constructor is provided
}
There is in fact no explicit constructor in the above snippet. What you have is a regular method that has the same name as the class. This is allowed, but discouraged:
JLS 8.4 Method Declarations
A
class
can declare a method with the same name as theclass
or a field, memberclass
or memberinterface
of the class, but this is discouraged as a matter of style.
You will find that if you create a new Parent()
or a new Child()
, "Yipppee!!!"
will NOT be printed to standard output. The method is not invoked upon creation since it's not a constructor.
Implicit super constructor parent() is undefined for default constructor. Must define an explicit constructor
In the future read the error messages first, and try to reason (or find) what it implies.
Constructors do not have a return type. Constructors are called to create an instance of the type. Essentially what is "returned" from a constructor is an instance of that type ready for use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With