Constructors

Constructor
  • After Creation of Object compulsory we should perform initialization then only that object in a position to provide service to others.
  • At the time of Object Creation some peace of code will execute automatically to perform initialization that peace of code is nothing but β€œConstructor”.
  • Hence the main Objective of constructor is to perform initialization.
  • If we don’t have constructor we will face burden. For example see the following code.
public class Student {
   String name;
   int rollno;
   public static void main(String[] args) {
      Student s1 = new Student();
      Student s2 = new Student();
      Student s3 = new Student();
      Student s4 = new Student();
      s1.name = "Ashok";
      s1.rollno = 101;
      
      s2.name = "Vinod";
      s2.rollno = 102;
      
      s3.name = "Dillesh";
      s3.rollno = 103;
      
      s4.name = "Thiru";
      s4.rollno = 104;
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
      System.out.println(s3.name+"-----"+s3.rollno);
      System.out.println(s4.name+"-----"+s4.rollno);
    }
}

To over come this type of burden constructor was introduced.

public class Student {
   String name;
   int rollno;
   Student(String name, int rollno) {
      this.name = name;
      this.rollno = rollno;
   }
   public static void main(String[] args) {
      Student s1 = new Student("raju",101);
      Student s2 = new Student("mani",102);
      System.out.println(s1.name+"-----"+s1.rollno);
      System.out.println(s2.name+"-----"+s2.rollno);
   }
}
Rules for Constructor

While writing constructors we should follow the following rules.

  1. The name of the constructor and name of the class must be same.
  2. The only allowed modifiers for the constructors are public, private, protected, <default>. If we are using any other modifier we will get C.E(Compiler Error).
  3. return type is not allowed for the constructors even void also. If we r declaring return type then the compiler treats it as a method and hence there is no C.E and R.E(RuntimeError).
public class Test {
   void Test() {
      System.out.println("Hai .....");
   }
   public static void main(String arg[]) {
      Test t = new Test();
   }
}

It is legal (But Stupid) to have a method whose name is exactly same as class name. Here it was treated as method.

Default Constructor

If we are not writing any constructor then the compiler always generate default constructor. If we r writing at least one constructor then the compiler won’t generate any constructor. Hence every class contains either programmer written constructor or compiler generated default constructor but not both simultaneously.

Prototype of default constructor

1. It is always no-arg constructor.

2. It contains only one – line super();

This is a call to super class – constructor it is no-argument call only.

3. The modifier of the default constructor is same as class modifier(either public or default).

The first line inside any constructor must be a call to super class constructor(super()) or a call to overloaded constructor of the same class(this()). If we are not taking any thing then the compiler will always place super().

super() & this() in constructor

We should use as first statement in constructor. We can use either super or this but not both simultaneously. Outside constructs we can’t use i.e we can invoke a constructor directly from another constructor only.

Overloaded Constructor

A class can contain more than one constructors with different arguments. This type of constructors are called β€œoverloaded constructor”.

public class Test {
   Test() {
      this(10);
      System.out.println("No-arg constructor");
   }
   Test(int i) {
      this(10.5);
      System.out.println("int-arg");
   }
   Test(double d) {
      System.out.println("double-arg")
   }
   public static void main(String arg[]) {
      Test t1 = new Test();
      Test t2 = new Test(10);
      Test t3 = new Test(20.5);
      Test t4 = new Test('a');
      Test t5 = new Test(10l);
   }
}

Inheritance concept is not applicable for constructor and hence overriding is also not applicable. If the parent class constructor throws some checked exception. Compulsory the child class constructor should throw the same checked exception or it’s parent other wise compile time error. If the parent class constructor throws unchecked exception then child class constructor not required to throw that exception.

Constructors


Scroll to top