Computer Applications
Answer
A constructor is a member method having the same name as that of a class and is used to initialise the instance variables of the objects. It is invoked at the time of creating any object of the class. For example:
Employee emp = new Employee();
Here, Employee() is invoking a default constructor.
Related Questions
State whether the following statement is True or False :
Copy constructor copies functions from one object to another.
While creating a class, a specific method called constructor is defined to initialise the instance variables. Such method may or may not use parameters. In a case where the constructor is not defined in the class, the system creates on its own to put the default initial value to the instance variables. The initial values can also be duplicated from one instance to another.
Based on the above discussion, answer the following questions:
(a) Which name resembles with the constructor name?
(b) What type of constructor is used with parameters?
(c) What type of constructor initialises the instance variables with default value?
(d) Which constructor is used to duplicate the initial values from one constructor to other?
Name the different types of constructors used while defining a class.
Why do we need a constructor as a class member?