05 Java - Constructor

Constructor

  • It used to create an instance and initialize the instance variable.

  • its similar to method expect:

    • Name: Constructor name is same as Class Name

    • Return Type: Constructor do not have any return type.

    • Constructor can not be static or final or abstract, synchronized.

  • The new keyword instructs Java during runtime to invoke the constructor rather than a method.

    ```java public class Employee {

    int empId;

public Employee(){ //Constructor }

public void Employee(){ //method // We can method with same name as class name //Not a best practice }

public Employee Employee(){ //method // We can method with same name as class name //Not a best practice } }



## FAQ

### Why constructor do not have return type?

(and Why constructor name is same as class name?)

* The constructor name is the same as the class name because it is easy to identify.

* Constructors do not have any return type, but Java implicitly adds the class type as the return type. If we were to include any return type, then it would become a method.

* Having the same name and no return type distinguishes the constructor from a method.


### Why constructor can not be final?

If a method is declared as final, it cannot be overridden by child classes. Child classes inherit all the variables and methods, but not the constructor. Let's assume that the constructor is also inherited by the child class. In the child class, the inherited constructor is treated as a method because the parent constructor's name is different from the child class name, and now we need to define the return type as well. Hence, constructors cannot be inherited. If constructors cannot be inherited, then how can we override them? So, what is the use of final?

### Why constructor can not be abstract?

To create an abstract method, we need an abstract class. The responsibility for implementing the abstract class lies with the child class. Since constructors cannot be inherited, how do we provide the implementation of a constructor in the child class?

### Why constructor can not be static?

Assuming the constructor is defined as static, it means the constructor can only access static variables. Consequently, we cannot initialize the instance variables, nor can we use `super()` for constructor chaining.

```java
public class Employee {
    int empId;
    public static Employee(int value){
        // Constructor cannot be static -- Error
        empId = value;
    }
}

Can we define constructor in interface?

We cannot create an object of an interface, so a constructor is not required for it.

Types of Constructor

1. Default

If the user does not provide any constructor, then by default, Java provides one


public class Employee {
    int empId; 
}

/*
Java adds the no arg constructor indirectly

public class Employee {

    public Employee() {
    }
}
*/

2. No Arg

If the constructor doesn't have any parameters then it is no argument.

A default constructor is not added when we have any type of user-defined constructor.

public class Employee {
    int empId;
    public Employee() {
        empId = 20;
    }
}

3. Parameterized

public class Employee {
    int empId;
    public Employee(int id) {
        empId = id;
    }
}

4. Constructor Overloaded

public class Employee {

    int empId;
    String name;
    String pincode;

    public Employee() {
        name = "chetan";
    }

    public Employee(int id, String empName) {
        empId =id;
        name = empName;
    }

    public Employee(int empId, String name, String pincode) {
        this.empId =empId;
        this.name = name;
        this.pincode = pincode
    }
}

5. Private Constructor

If the constructor is set to private, only the methods inside the class can access it. To obtain an object of this class, we can create the object within a class method and utilize this method to retrieve the object. This method should be static because we cannot create an object of this class from outside, given that the constructor is private.

public class Employee {

    int empId;
    String name;

    private Employee() {
    }

    public static Employee getInstance(){
        return new Employee();
    }
}

6. Constructor Chaining

1. this

The this keyword can be used inside the particular class. With its help, we can call any constructor present in the class.

public class Employee {

    int empId;
    String name;

    public Employee() {
        this(10);
    }

    public Employee(int empId) {
        this(empId, "CD");
    }

    public Employee(int empId, String name) {
        this.empId = empId;
        this.name = name;
    }
}

2. super

Internally, the parent constructor is first invoked using the super keyword. If we manually include a super keyword call, it should be placed at the beginning of the method. For a no-argument constructor, there is no need to include super, but for a parameterized constructor, calling the super keyword with valid arguments is mandatory.

public class Person {
    public Person() {
        System.out.println("Inside Person class constructor");
    }
}

public class Doctor extends Person{
    public Doctor() {
        System.out.println("Inside Doctor class constructor");
    }
}

Doctor doctor = new Doctor();

/*
Output -
Inside Person class constructor
Inside Doctor class constructor

Reason---
Due to hidden super keyword.

public class Doctor extends Person{
    public Doctor() {
        super();
        System.out.println("Inside Doctor class constructor");
    }
}
*/
public class Person {
    public Person() {
        System.out.println("Inside Person class constructor");
    }
}

public class Doctor extends Person{
    public Doctor() {
        super();
        System.out.println("Inside Doctor class constructor");
    }
}

Doctor doctor = new Doctor();

/*
Output -
Inside Person class constructor
Inside Doctor class constructor
*/
public class Person {
    String name;
    public Person(String name) {
        this.name = name;
    }
}

public class Doctor extends Person{

    int empId;
    public Doctor(int empId) {
        super("Doctor");
        this.empId = empId;
    }
}