L21: Constructors

Q1. What is a constructor ?

A constructor is a special type of subroutine or method that is automatically called when an object is created. It is used to initialize the object's state immediately after creation. Constructors have the same name as the class and may accept parameters to initialize the objects with specific values.

Q2. What is a default constructor ?

If no constructors are explictly defined in a class then, the java compiler implictly defines a constructor with an empty body and without any parameter. This consturctor is called as default constructor. The purpose of default constructor is to initialize the instance variables of a class to their default values. The default values for numeric types (byte, short, int, long, float, double), boolean type, char type, and reference types (objects) are 0 (zero), false, \u0000 (null character), and null, respectively.

Q3. How a constructor is different from a method in java ?

Constructors and methods serve different purposes and they have different characteristics:

Q4. How do you declare a constructor in a java class ?

Following syntanx is used to declare a constructor inside a java class, say MyClass.

class MyClass {
  //instance variables
  //constructor
  MyClass() {
    //body of constructor
  }
  // other methods
}

The constructor name must be exactly same as the class name. Constructors do not have any return type, not even void. The codes for initializing objects are written in the body of the constructor. Other necessary tasks required at the time of object creation can also be written in the body of the constructor.
Parameterized constructors can also be created by adding parameters to a constructor as follows:

MyClass (parameter list) {
  //body of constructor
}

Q5. Create a class Employee with instance variables name, age and salary. Initialize these variables using a parameterized constructor. Implement a method display() to print the details of an Employee instance.

class Employee {
  String name;
  int age;
  int salary;
  Employee(String name, int age, int salary) {
    this.name=name;
    this.age=age;
    this.salary=salary;
  }
  void display() {
    System.out.println("Employee Details");
    System.out.println("Name is: "+name);
    System.out.println("Age is: "+age);
    System.out.println("Salary is: "+salary);
  }
}
class Test {
  public static void main(String[] args){
    Employee e1=new Employee("Raju", 25, 9999);
    e1.display();
    Employee e2=new Employee("Rani", 23, 4999);
    e2.display();
  }
}

Q6. What is constructor overloading ? Explain with an example.

Constructor overloading allows a class to have multiple constructors with different parameter lists. Thus, each construcor works differently to initialize different objects of the class. Consider the following example to understand the concept of constructor overloading.

class Employee {
  String name;
  int salary;
  Employee() {
    this.name="unknown";
    this.salary=0;
  }
  Employee(String name, int salary) {
    this.name=name;
    this.salary=salary;
  }
  Employee(String name, int reward, int day) {
    this.name=name;
    this.salary=reward*day;
  }
  void display() {
    System.out.println("Employee Details");
    System.out.println("Name is: "+name);
    System.out.println("Salary is: "+salary);
  }
}
class Test {
  public static void main(String[] args) {
    Employee e1=new Employee();
    Employee e2=new Employee("Raju", 9999);
    Employee e3=new Employee("Rani", 100, 25);
    e1.display();
    e2.display();
    e3.display();
  }
}

In this program, depending on the object creation appropriate constructor gets invoked. For object e1, the constructor Employee() gets invoked. For object e2, the construcor Employee(String, int) gets invoked. For object e3, the construcor Employee(String, int, int) gets invoked.