Constructors and Constructor overloading

Constructors and Constructor overloading


A constructor looks more like a method but without return type. Moreover, the name of the constructor and the class name should be the same. The advantage of constructors over methods is that they are called implicitly whenever an object is created. In case of methods, they must be called explicitly. To create an object, the constructor must be called. Constructor gives properties to an object at the time of creation itself (else, it takes some method calls with extra code to do the same job). Programmer uses constructor for initializing variables, instantiating objects and setting colors. Constructor is equivalent to init() method of an applet.

Default Constructor – No Argument Constructor

A constructor without parameters is called as "default constructor" or "no-args constructor". It is called default because if the programmer does not write himself, Java creates one and supplies. The default constructor supplied does not have any functionality (output).

Output screen of Demo.java

public Demo()

"public" is the access specifier and "Demo()" is the constructor. Notice, it does not have return type and the name is that of the class name.

Demo d1 = new Demo();

In the above statement, d1 is an object of Demo class. To create the object, the constructor "Demo()" is called. Like this, any number of objects can be created like d2 and for each object the constructor is called.

Constructor Overloading

Just like method overloading, constructors also can be overloaded. Same constructor declared with different parameters in the same class is known as constructor overloading. Compiler differentiates which constructor is to be called depending upon the number of parameters and their sequence of data types.

Output screen of Perimeter.java


Perimeter constructor is overloaded three times. As per the parameters, the appropriate constructor is called. To call all the three constructors three objects are created. Using this(), all the three constructors can be called with a single constructor.

this() with Constructors

Suppose by accessing one constructor, the programmer may require the functionality of other constructors also but by creating one object only. For this, Java comes with this(). "this()" is used to access one constructor from another "within the same class". Depending on the parameters supplied, the suitable constructor is accessed.

Output screen of Perimeter.java


In the code, creating object p3, the III constructor is accessed. From III, with "this(100)" statement, the II constructor is accessed. Again from II, the I is accessed without the statement "this()". As per the parameter supplied to this(), the appropriate or corresponding constructor is accessed.

Rules of using this()

A few restrictions exist for the usage of this().

  1. If included, this() statement must be the first one in the constructor. You cannot write anything before this() in the constructor.
  2. With the above rule, there cannot be two this() statements in the same constructor (because both cannot be the first).
  3. this() must be used with constructors only, that too to call the same class constructor (but not super class constructor).

47 thoughts on “Constructors and Constructor overloading

  1. s.s.sujitha

    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. akshay

    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

  3. sibaram

    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….

  4. Ravi

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

    1. S. Nageswara Rao, Corporate Trainer

      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.

  5. krishna

    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. S. Nageswara Rao, Corporate Trainer

      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. S. Nageswara Rao, Corporate Trainer

      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 Reply

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