ICSE Class 10 Computer Applications Question 13 of 19

Constructors — Question 13

Back to all questions
13
Question

Question 13

How do we invoke a constructor ?

Answer

A constructor is invoked automatically when an object of a class is created. The constructor is called using the new keyword followed by the name of the class and a set of parentheses. If the constructor requires arguments, the arguments are passed inside the parentheses.

For example, suppose we have a class named Person with a parameterized constructor that takes two arguments name and age. To create an object of this class and invoke the constructor, we would use the following code:

Person personObj = new Person("John", 30);