Mastering Constructors and Java Access Specifiers
What is a Constructor?
A constructor is a special member function of a class that is automatically invoked when an object of the class is created. Its main purpose is to initialize the data members of the class. In C#, a constructor has the same name as the class and does not have any return type, not even void.
Characteristics of a Constructor
- Same name as class: The constructor name must be exactly the same as the class name.
- No return type: Constructors do not return any value.
- Automatically called: It is invoked automatically when an object of the class is created.
- Used for initialization: Constructors initialize data members and allocate resources.
- Can be overloaded: Multiple constructors can exist in a class with different parameters.
- Default