Constructor is a special member function of a class that is used to initialize the object. If a class has a Constructor,
Java automatically calls that Constructor when an object is created.
The name of the Constructor is the same as the name of the class.
Output:-
Creating Sample 0
Creating Sample 1
Creating Sample 2
Creating Sample 3
Creating Sample 4
Characteristics of Constructor:-
- Constructor do not have return type or value.
- Constructor have same name as class name.
- Constructor cannot be inherited .
- java automatically calls the Constructor when an object is created.
Types Of Constructor:-
- Simple/default Constructor
- Parameterized Constructor
Simple/Default Java Constructor:-
Output:-
Creating SampleParameterized Java Constructor:-
Output:-
Creating Sample 0
Creating Sample 1
Creating Sample 2
Creating Sample 3
Creating Sample 4
No comments:
Post a Comment