About Constructor

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 only. programmer uses constructor to initialize 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, JVM creates one and supplies. The default constructor supplied by the JVM does not have any functionality (output).
1
2
3
4
5
6
7
8
9
10
11
12
public class Demo
{
  public Demo()
  {
    System.out.println("From default constructor");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    Demo d2 = new Demo();
  }
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Perimeter
{
  public Perimeter()                                                     // I
  {
    System.out.println("From default");
  }
  public Perimeter(int x)                                                // II
  {
    System.out.println("Circle perimeter: " + 2*Math.PI*x);
  }
  public Perimeter(int x, int y)                                       // III
  {
    System.out.println("Rectangle perimeter: " +2*(x+y));
  }
  public static void main(String args[])
  {
    Perimeter p1 = new Perimeter();                     // I
    Perimeter p2 = new Perimeter(10);                  // II
    Perimeter p3 = new Perimeter(10, 20);            // III
  }
}
Output screen of Perimeter.java

Perimeter constructor is overloaded three times. As per the parameters, an 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Perimeter
{
  public Perimeter()                                                      // I
  {
    System.out.println("From default");
  }
  public Perimeter(int x)                                                // II
  {
    this();
    System.out.println("Circle perimeter: " + 2*Math.PI*x);
  }
  public Perimeter(int x, int y)                                         // III
  {
    this(100);
    System.out.println("Rectangle perimeter: " +2*(x+y));
  }
  public static void main(String args[])
  {
    Perimeter p3 = new Perimeter(10, 20);            // III
  }
}
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).

0 comments:

Post a Comment