English

Answer the following in brief. Give a program code example wherever applicable. What is the use of a default constructor?

Advertisements
Advertisements

Question

Answer the following in brief. Give a program code example wherever applicable.

What is the use of a default constructor?

Very Long Answer
Advertisements

Solution

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
  Is there an error in this question or solution?
Chapter 4: Constructors - Exercises [Page 115]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 4 Constructors
Exercises | Q 3. (ii) | Page 115
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×