Constructors and Constructor overloading



Constructors in Inheritance

Constructors come with some implicit behavior. Most of the time, the programmer would like to have the behavior and properties of super class constructor also being in subclass. For this reason, "a subclass constructor calls super class default constructor implicitly".

class Hello
{
  public Hello()
  {
    System.out.println("From Hello constructor");
  }
} 
class Test extends Hello
{
  public Test()
  {
    System.out.println("From Test constructor");
  }
} 
public class Demo extends Test
{
  public Demo()
  {
    System.out.println("From Demo constructor");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
  }
}

Output screen of Demo.java

In the above program Demo extends Test and in turn Test extends Hello. It is multilevel inheritance. In the main() method, we are accessing Demo() constructor by creating the object d1 of Demo class. The Demo() constructor calls implicitly Test() constructor because it is the super class of Demo. Similarly Hello is the super class of Test and thereby Test() constructor calls Hello() constructor. Now you get the output of all the three constructors. The output you get in reverse order.

super() with Constructors

We came to know in the previous program that subclass constructor calls super class constructor implicitly. Let us make the rule more clear. Either the "default or the overloaded constructor of the subclass calls super class default constructor implicitly". If the programmer would like to call the overloaded constructor of the super class from subclass constructor, he can use super(). Compiler comes to know which
super class constructor is to be called depending upon the parameter list of super().

class Test
{
  public Test()                        // I
  {
    System.out.println("From Test default");
  }
  public Test(int x)                   // II
  {
    this();
    System.out.println(x);
  }
}
public class Demo extends Test
{
  public Demo()                        // III
  {
    super(100);
    System.out.println("From Demo default");
  }
  public Demo(int x)                   // IV
  {
    this();
    System.out.println(x);
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo(10);            // IV
  }
}

Output screen of Demo.java

In the above code, super class Test and subclass Demo come with two constructors each – one default and one overloaded.

this();

We know earlier, this() calls same class default constructor as there are no parameters.

super(100);

From III constructor, we are calling super class II constructor with super(100). "super(100)" calls super class matching integer parameter constructor, I.

if the statement super(100) is omitted in III, the III calls implicitly I.

Rules of using super()

Same restrictions of this() exist for the usage of super() also.

  1. If included, super() statement must be the first one in the constructor. You cannot write anything before super() in the constructor.
  2. With the above rule, there cannot be two super() statements in the same constructor (because both cannot be the first). That is, we can have only one of either this() or super() in the constructor.
  3. super() must be used with constructors only, that too to call the super class constructor from subclass constructor.

47 thoughts on “Constructors and Constructor overloading”

  1. class Emp
    {
    int id;
    int salary;
    int age;

    Emp()
    {
    id=34;
    salary=900;
    age=45;
    }
    Emp(int e)
    {
    id=salary=age=e;
    }
    Emp(int x,int y,int z)
    {
    x=id;
    y=salary;
    z=age;
    }
    }
    public static void main(String args[])
    {
    int s1,s2,s3;
    Emp s1=new Emp();
    Emp s2=new Emp(12);
    Emp s3=new Emp(23,4555,56);
    }
    }

    Sir,Is my coding is right? if not please correct it.

    and also i have a doubt,where should i give my print statement?

  2. Dear sir,
    Why method binding is done at run time. while it can be done at compile time based on referred type object.

    thanks

  3. sir..your blog is just amazing.. Can i have pdf version of your notes.. because i don,t have net connection in my phone.. its not easy to read from laptop all the time

  4. hello sir…

    i have doubt about constructor….
    if in our class does not have a constructor then JVM creates internally default constuctor as per your blog….

    but i learned some trainer’s said that ,if in our class does not have a constructor then compiler rendering default constructor internally….

    then,what is the exact answer?
    please reply my question……

    thanks….

  5. class Animal {
    Animal(String name) { }
    }
    class Horse extends Animal {
    Horse() {
    super(); // Problem!
    }
    }
    will this code works??
    How to make this code to work??

    1. class Animal
      {
      Animal() { }
      Animal(String name) { }
      }
      class Horse extends Animal
      {
      Horse()
      {
      super();
      }
      }
      Reason of your problem: The subclass constructor calls super class default constructor implicitly. In the super class you have overloaded the constructor. In this case, default will not created by JVM implicitly. You must provide your own default constructor. That what I have done.

  6. Sir, as you said constructor can be called implicitly and method should be called explicitly.please explain in detail difference between calling a constructor or a method implicitly and explicitly with an example.

    1. Suppose you write a method display(). Without calling from something like from main(), it is not executed. That is, forgetting to call, the method is never executed.

      Incase of constructor, you write the constructor and just leave it. Because definitely you create atleast one object, the constructor is automatically called. That is, when object is created the constructor is called automatically.

    1. No meaning of overriding a constructor. A constructor should declared and given body in the same class and not in some other class. That is, we say, constructors cannot be overridden but can be accessed from sub class. For this reason, constructors are not members of a class; only methods and variables are members.

Leave a Comment

Your email address will not be published. Required fields are marked *