Advertisements
Advertisements
प्रश्न
Answer the following in brief. Give a program code example wherever applicable.
What is the use of a default constructor?
विस्तार में उत्तर
Advertisements
उत्तर
A default constructor is a no-argument constructor that either the programmer defines explicitly or the Java compiler provides automatically when no other constructor is written in the class.
Key Uses of a Default Constructor:
- Object Creation: It allows the program to instantiate an object of a class using the new keyword without passing any initial values (e.g., new ClassName()).
- Default Initialisation: It assigns predictable, default starting values to all instance variables of the object (such as 0 for numeric types, false for booleans, and null for objects).
- Parent Class Linkage: It automatically calls the constructor of the parent class (super()), ensuring that the entire object hierarchy initializes properly.
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: Constructors - Exercises [पृष्ठ ११५]
