Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have a return type for a constructor in Java?

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?

like image 511
user421814 Avatar asked Aug 17 '10 11:08

user421814


4 Answers

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();
 }
}
like image 54
codaddict Avatar answered Oct 20 '22 06:10

codaddict


On constructor not having return type

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.


On default constructors

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);
   }

}

On methods having the same name as the constructor

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 the class or a field, member class or member interface 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.

like image 23
polygenelubricants Avatar answered Oct 20 '22 05:10

polygenelubricants


  • when you extend a class that does not have a default constructor, you must provide a constructor that calls that superconstructor - that's why the compilation error, which is:

Implicit super constructor parent() is undefined for default constructor. Must define an explicit constructor

  • when you add a return type this is no longer a constructor, it's a method, and the above does not apply

In the future read the error messages first, and try to reason (or find) what it implies.

like image 5
Bozho Avatar answered Oct 20 '22 05:10

Bozho


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.

like image 2
Noel M Avatar answered Oct 20 '22 06:10

Noel M