Java - OOP - Constructors
A constructor is a special method in Java that is used to initialize objects. It is called at the time of object creation. It has the same name as the class and no return type, not even void.
There are three types of constructors:
Default Constructor: If we do not declare any constructor in a class, then a default constructor is provided by the Java compiler. It does not have any parameter.
public class Student {
String name;
int rollNo;
public Student() {
name = "John";
rollNo = 1;
}
}
Parameterized Constructor: It is used to initialize the instance variables of a class with the help of arguments passed at the time of object creation.
public class Student {
String name;
int rollNo;
public Student(String n, int r) {
name = n;
rollNo = r;
}
}
Copy Constructor: It is used to create a new object with the same values as the existing object.
public class Student {
String name;
int rollNo;
public Student(Student s) {
name = s.name;
rollNo = s.rollNo;
}
}
Constructors can also be overloaded like methods.
public class Student {
String name;
int rollNo;
public Student() {
name = "John";
rollNo = 1;
}
public Student(String n) {
name = n;
rollNo = 1;
}
public Student(int r) {
name = "John";
rollNo = r;
}
public Student(String n, int r) {
name = n;
rollNo = r;
}
}
In the above example, we have defined four constructors: a default constructor, a parameterized constructor with one argument, a parameterized constructor with one integer argument, and a parameterized constructor with two arguments. We can create objects using any of these constructors based on our requirement.
Advantages of Constructors:
- Constructors are used to initialize objects with values.
- Constructors help to provide the default values of instance variables.
- Constructors help to create objects with default and parameterized values.
Basic terms related to constructors:
- Constructor overloading: It is the process of defining more than one constructor with different parameters in a class.
- Constructor chaining: It is the process of calling one constructor from another constructor using the keyword "this".
- Super keyword: It is used to refer to the parent class constructor, parent class instance variables, and parent class methods from the child class.
- Default constructor: It is a constructor provided by the Java compiler when we do not declare any constructor in a class. It does not have any parameter.
- Parameterized constructor: It is a constructor that takes parameters to initialize the instance variables of a class.
- Copy constructor: It is a constructor used to create a new object with the same values as the existing object.
public class Car {
String make;
String model;
int year;
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
}
In this example, we have a Car class with three instance variables: make, model, and year. We also have a constructor for the Car class that takes in three parameters: make, model, and year.
The constructor uses the this keyword to refer to the instance variables of the object being created. The values of the make, model, and year parameters are assigned to the make, model, and year instance variables, respectively.
Now, when we create a new Car object, we can pass in the make, model, and year values as arguments to the constructor:
Car myCar = new Car("Toyota", "Corolla", 2021);
This will create a new Car object with the make set to "Toyota", the model set to "Corolla", and the year set to 2021.
Constructors are useful for initializing instance variables and performing any other necessary setup for objects when they are created.