What is Constructor?
- A constructor is a special block in a class that runs automatically when an object is created.Its job is to set initial values for the object.
Can we call the constructor Manually?
- No,It runs automatically when we create an object using new.
How to create a constructor?
- Constructor name must be the same as the class name.
Example :
class Student {
int id;
Student(int id) {
this.id = id; // instance variable = local variable
}
}
Is it allowed to mention return type for a constructor?
- No return type (not even void).
What is the purpose of constructor?
- Used to set initial values of instance variables. By using this keyword, we can access/assign values to the instance varaiable inside constructor. this refers to current object only. So this keyword is used to differentiate instance variables from local variables
Is constructor overload allowed?
- Yes, it can be overloaded.
Does java provide any default constructor?
- Yes, If you donβt write one, Java provides a default constructor.
How many types of constructor available?
- Default constructor
- No-Argument Constructor
- Parameterized constructor
What is Default constructor?
- If we do not create any constructor, the Java compiler automatically creates a no-arg constructor during the execution of the program. The default constructor initializes any uninitialized instance variables with default values.
What is No-Argument Constructor?
- A Constructor with no arguments is called No-Argument Constructor.
class Company {
String name;
Company() {
this.name = "Payilagam";
}
public static void main(String[] args) {
Company obj = new Company();
System.out.println("obj.name");
}
}
What is Parameterized constructor?
- A constructor with parameters. A Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors.
class Bank {
String name;
String mailId;
int userID;
// constructor with two parameters
Bank(String name,int userID) {
this.name = name;
this.userID = userID;
}
// constructor with three parameters
Bank(String name,int userID,String mailId) {
this.name = name;
this.userID = userID;
this.mailId = mailId;
}
public static void main(String[] args) {
// call constructor by passing two values
Bank obj1 = new Bank("Nanthini",1);
// call constructor by passing three values
Bank obj2 = new Bank("Kani",2,"kani@gmail.com");
}
}
What is Constructor overloading?
- Creating more than one constructor in the same class with different parameters is called Constructor overloading.
Real-world Scenario 1:
When you open a bank account, details like account number, name, and balance must be set at the time of creation.
Why constructor here?
1.Account cannot exist without details
2.Values are initialized immediately
3.No chance of creating an incomplete object
Scenario 2: Student Registration :
Real-world Scenario 2:
When a student is registered, roll number and name are mandatory.
So they must be passed during object creation.
Why constructor here?
1.Student must have roll number
2.Prevents invalid objects like Student() with no data
Top comments (0)