(a) Constructor with default argument - Java specification doesn't support default arguments in methods so Constructor with default argument cannot be written in Java. Why do we need a constructor as a class member?Ī constructor is used to initialize the objects of the class with a legal initial value. The different types of constructors used while defining a class are as follows: Name the different types of constructors used while defining a class. Here, Employee( ) is invoking a default constructor. It is invoked at the time of creating any object of the class. (d) Copy constructor Answer the following Question 1Ī 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. (d) Which constructor is used to duplicate the initial values from one constructor to other? (c) What type of constructor initialises the instance variables with default value? (b) What type of constructor is used with parameters? (a) Which name resembles with the constructor name? The initial values can also be duplicated from one instance to another.īased on the above discussion, answer the following questions: 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. Such method may or may not use parameters. While creating a class, a specific method called constructor is defined to initialise the instance variables. The compiler supplies a special constructor in a class that does not have any constructor.Ī constructor is not defined with any return type.Įvery class must have all types of constructors.Ī constructor is a member method of a class.Ĭonstructor is used to initialize the data members of a class.Ī constructor may have different name than the class name.Ī constructor is likely to be defined after the class declaration.Ĭopy constructor copies functions from one object to another.įalse Case-Study based question Question 1 The constructor generated by the compiler is known as default constructor. The argument of a copy constructor is always passed by reference. The this keyword refers to the current object. Parameterised constructor creates objects by passing value to it. Question 3Ī constructor is used when an object is created Question 4Ī constructor without any argument is known as non-parameterised constructor. Question 2Ī constructor has no return type. Fill in the blanks Question 1Ī member function having the same name as that of the class name is called constructor. Reason - A constructor has no return type as it doesn't return any value. It is used only to initialize the data members. Reason - Default constructor initialises data members with default values. Which constructor initialises data members with default values? Reason - A constructor is always public because it is always called from outside the class while creating an object. Reason - A constructor has no return type as its primary function is to set initial values of data members at the time of creation of an object. A constructor is not used for arithmetical and logical operations.A constructor is called while creating an object.A constructor has same name as class name.Which of the following statements is false? Reason - A constructor is used to initialise data members with default or user-defined values at the time of creation of an object. Chapter 7 Constructors Class 10 - APC Understanding Computer Applications with BlueJ
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |