Constructor:
A constructor is similar to a method because it also has a name and can take parameters.
The name of the constructor is always same as class name.
We have variables inside a class, and the constructors are used to initialize those variables with initial values during object creation.
we do not call the constructors explicitly, they are automatically called by the JVM when an object is created.
Constructor does not return any value even void. that no return type.
Example-1:
public class HelloWorld {
public static void main(String[] args) {
BankAccount acc = new BankAccount();// here we create the object with new keyword.
//Here the default constructor will be called that is -> BankAccount()
//output : Default constructor is called
}
}
class BankAccount {
BankAccount() { // default constructor
System.out.println("Default constructor is called");
}
}Example-2:
public class HelloWorld {
public static void main(String[] args) {
BankAccount acc = new BankAccount("Ashish", 5000);// initialized during object creation.
}
}
class BankAccount {
String accountHolder;
double balance;
// constructor initializes account details
BankAccount(String name, double initialBalance) { // name and initialBalance are the local variable.
accountHolder = name;// here we are initializing the variable.
balance = initialBalance;// here we are initializing the variable.
}
}There are 2 types of constructor:
- Default constructor
- Parameterized constructor
1. Default constructor:
The constructor whic does not contain any parameter , we call it as default constructor.
When we do not define any constructor, the Java compiler automatically provides a default constructor. If we define a constructor, the JVM does not create the default constructor.
Example-1:
public class HelloWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
}
}
class MyClass {
// JVM defines default constructor which is not visible in the code but is automatically provided.
//Invisible default constructor is provided by JVM.
/* MyClass() {
// default constructor provided by JVM
} */
}Example-2:
public class HellowWorld {
public static void main(String[] args) {
MyClass obj = new MyClass();
}
}
class MyClass {
// no-argument constructor
MyClass() {
System.out.println("Default constructor is called");
}
}1. Parameterized constructor:
Like a Java method, a constructor can also have parameters. It is used to initialize variables with some initial values during object creation.
Example:
public class HelloWorld {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = new Student("Bob", 22);
s1.display();
s2.display();
}
}
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}Constructor Over Loading:
we can define multiple constructor inside a class which is known as constructor overloading. Here the constructor parameter should be different with respect to number or parameters, type of parameters, order of parameters.
Example-1: Number of parameters are different:
public class HelloWorld {
HelloWorld(int number1) {
System.out.println("Constructor with one parameter");
}
HelloWorld(int number1, int number2) {
System.out.println("Constructor with two parameter");
}
HelloWorld(int number1, int number2, int number3) {
System.out.println("Constructor with three parameter");
}
}Example-2: Type of parameters are different:
public class HelloWorld {
HelloWorld(int number1) {
System.out.println("Constructor with int type parameter");
}
HelloWorld(String name) {
System.out.println("Constructor with string type parameter");
}
HelloWorld(boolean isTrue) {
System.out.println("Constructor with boolean type parameter");
}
}Example-2: Order of parameters are different:
public class HelloWorld {
HelloWorld(int number1 , String name) {
System.out.println("Constructor with parameter order: int, string");
}
HelloWorld(String name, int number1) {
System.out.println("Constructor with parameter order: string, int");
}
}Example-4: Number of parameters, order of parameters and type of parameters are different:
public class HelloWorld {
HelloWorld(int number1) {
System.out.println("Constructor with one parameter");
}
HelloWorld(int number1, int number2) {
System.out.println("Constructor with two parameter");
}
HelloWorld(int number1 , String name) {
System.out.println("Constructor with parameter order: int, string");
}
HelloWorld(String name, int number1) {
System.out.println("Constructor with parameter order: string, int");
}
HelloWorld(String name) {
System.out.println("Constructor with string type parameter");
}
HelloWorld(boolean isTrue) {
System.out.println("Constructor with boolean type parameter");
}
}Constructor Chaining In Java:
If there are multiple constructors inside a class, we can call one constructor from another constructor, This process is called Constructor Chaining in Java.
This allows us to reuse the code of one constructor in another constructor, reducing code duplication and helps code reusability.
There are 2 type of constructor chaining :
- Constructor Chaining in the Same Class
- Constructor Chaining Between Parent and Child Classes
Constructor Chaining in the Same Class:
We can create multiple constructor inside a same class and can call one constructor inside another constructor.
To call the constructor in the same class we need to use this keyword.
Important : this keyword must be the first statement in a constructor.
Example-1:
public class HelloWorld {
public static void main(String[] args) {
MyClass h1 = new MyClass();
//Output:
//One-parameter constructor executed
//Default constructor executed
}
}
class MyClass {
// Constructor 1: No parameter or default constructor
MyClass() {
this("Hello"); // calls Constructor 2
System.out.println("Default constructor executed");
}
// Constructor 2: One parameter
MyClass(String msg) {
System.out.println("One-parameter constructor executed");
}
}
Explanation:
When we create the object that is -> MyClass h1 = new MyClass();
the default constructor will be called that is -> MyClass().
Here inside the default constructor or constructor-1 we have called 2nd constructor that is -> this("Hello"); So immediately it will call the 2nd constructor.
So the output is :One-parameter constructor executed
Default constructor executed
See you in next page....🙂