Constructors
Constructors are special methods within a class that are used to initialize the state of an object when it is created. Constructors have the same name as the class and do not have a return type, not even void.
Default Constructor
Parameterized Constructor
You can define constructors that accept parameters to initialize the object with specific values.
Constructor Overloading
You can define multiple constructors with different parameter lists within the same class. This is called constructor overloading.
`this` keyword:
`this` is actually a pointer which refers to the same class in which it is present.
For example, we are present in a class named "Person". It has an attribute "age". Now whenever we write this.age, we are specifically targeting to the age variable of the Person class.
Similarly, if we are present in Employee class and it has a variable named "salary". So, whenever we write this.salary, we are specifically targeting to the salary variable of the Employee class.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP