Overloaded constructors exist in Java. Within a class, we can declare multiple constructors. All these constructors are having the same name but different type of arguments. Hence, all these constructors are considered overloaded constructors. Hence, overloading concept is applicable to constructors.
Example of Overloaded Constructors
// Demo class class Demo { // defining three overloaded constructors // No arguments constructor Demo() { // calling integer arguments constructor of the same class this(100); System.out.println("No-arguments constructor called"); } // integer argument constructor Demo(int i) { // calling double arguments constructor of the same class this(4.13); System.out.println("integer-argument constructor called"); } // double argument constructor Demo(double i) { System.out.println("double-argument constructor called"); } public static void main(String[] args) { // creating demo class objects using different constructors Demo d1 = new Demo(); Demo d2 = new Demo(50); Demo d3 = new Demo(5.66); } }
Here, every constructor when calls this(), control flow first executes that constructor and then return back to the original constructor.
Hence, the above program will have following output.
Output:
double-argument constructor called integer-argument constructor called No-arguments constructor called double-argument constructor called integer-argument constructor called double-argument constructor called
For constructors, inheritance and overriding concepts are not applicable. But overloading concept is applicable.
Every class in Java including abstract class can contain constructor but an interface cannot contain constructor.
An interface doesn’t contain any instance variables. It only contains static variables. Hence, an interface doesn’t need a constructor since constructor’s main aim is to initialize instance variables.
Recursive call in constructors
If there is a possibility of recursive constructor invocation, then the code will not be compiled successfully.
// Demo class class Demo { // defining two overloaded constructors which recursively call each other. // No arguments constructor Demo() { // calling integer arguments constructor of the same class this(100); } // integer argument constructor Demo(int i) { // calling no-arguments constructor of the same class this(); } // main method public static void main(String[] args) { System.out.println("main method is executing"); } }
Running above program will give us a compile-time error saying recursive constructor invocation.
However, if we recursively call two methods instead of constructors, it will not give us a compile-time error. Instead, it will give us a runtime error – Stack Overflow Error.
// parent class class Parent { // parent class integer-argument constructor Parent(int i) { } } // child class class Child extends Parent { }
When we run the above code, we will get a compile-time error saying Implicit super constructor Parent() is undefined for default constructor. Must define an explicit constructor.
Note that, if parent class contains any argument constructor, then while writing child classes, we have to take special care with respect to constructors.
Whenever we are writing any argument constructor, it is highly recommended to write no-arguments constructor also since default constructor of child class will always invoke the super-class no-arguments constructor.
An example involving Constructor and Exception Handling
// parent class class Parent { // parent class integer-argument constructor Parent(int i) throws IOException { } } // child class class Child extends Parent { }
Running the above code will give us a compile-time error saying unreported exception java.io.IOException in the default constructor.
Reason: The child class default constructor will call the superclass constructor. Since our parent class constructor throws IOException (checked exception), the child constructor must handle that exception while calling parent constructor.
If parent class constructor throws any checked exception, child class constructor must throw the same checked exception or its parent. Otherwise, the program will not compile.