0% found this document useful (0 votes)
17 views3 pages

Constructor

The document discusses constructors in object-oriented programming. It defines what a constructor is, its uses, and different types of constructors like default and parameterized constructors. It also covers topics like constructor overloading, the difference between methods and constructors, and the use of the 'this' keyword in constructors.

Uploaded by

simakumari18919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Constructor

The document discusses constructors in object-oriented programming. It defines what a constructor is, its uses, and different types of constructors like default and parameterized constructors. It also covers topics like constructor overloading, the difference between methods and constructors, and the use of the 'this' keyword in constructors.

Uploaded by

simakumari18919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Constructor

Q1) What is a constructor? What is its use?


Ans :- A constructor is a special member method of a class that is invoked automatically whenever the
class is instantiated. A constructor initializes the data member and environment for the object that is
being created.

Q2) State the features of a constructor.


Ans :- a)The name of the constructor is same as that of a class.
b) It does not have a return type not even “void”.
c) It is a self-calling method which is invoked automatically during object creation.
d) Constructors are generally defined as public so that it can be accessed by any other class also.

Q3) What is a default constructor?


Ans :- A constructor which does not take any parameter is known as a default Constructor. It is used to
initialize data members by the default values provided by i) the program ( user ) or ii) java ( default
values of data types).

Q4) What is the need of parameterized constructor?


Ans :- Sometimes the user needs to create different objects with different values of data members. This
is possible by using parameterized constructor and passing different arguments for creating different
objects.

Q5) What happens when the class does not contain any constructor?
Ans :- If a class does not contain any constructor, then compiler creates a default constructor which
initializes data members for the object using default values provided by java for different data types.

Q6) What is the advantage of Parameterized Constructor over Default


Constructor?
Ans :- Using parameterized constructor, different objects can be created with different states / values.

Q7) What is the return type of Constructor?


Ans :- The return type of a Constructor, by default, is the class itself.

Q8) Can a Constructor be declared as private? If so what is its use?


Ans :- Yes, A constructor declared as private can be used by the member methods of the class only, and
not outside the class.
Q9) What is “this” keyword? What is its significance?
Ans :- “this” keyword refers to the current object. It stores the address of the object in use currently.

Uses / significance:-
i) it is useful in returning the object address of which the function is a member.
ii) to refer to the data member, in case it matches with the local variable.
iii) used in constructor to initialize the data members.

Example: -
i) Class Rectangle

{ Int length; int breadth;// data members

double diagonal;

Rectangle()//default constructor

{ this(10,8,10.0); }

Rectangle( int length, int breadth, double diagonal)// parameterized constructor

{ this.length=length;

this.breadth=breadth;

this.diagonal=diagonal;

ii) Class Student

{ int roll; ;// data members

double average;// data members

Student()// used in constructor to initialize data members

this ( 1, 0.0);

void input( int roll, double average)// referring to the data member, if it matches with the local variable

{ this.roll= roll;

this. average= average;

}}
Q 10) What is Constructor Overloading? What is its need?
Ans :- When more than one constructor is used in a class, it is called Constructor Overloading. Each
Constructor should have different signature (Parameter List).

Sometimes it is required to create an object using default Constructor and sometime using
parameterized constructor.

Q 11) What are the differences between a method (function) and a Constructor?
Parameters Method ( Function) Constructor
Name Functions have name other than class name. Constructor has name same as that of the
class.
Need To avoid repetition and to increase code To initializes the member variables.
reusability.
Return type They have a return type specifier. They do not have a return type specifier.
Specifier
Return value It may or may not return any value. It does not return any value, not even
void.
Order of It can be executed repeatedly as and when It gets invoked automatically at the time
execution required by giving a call to method name. of creating an object of the class.

**************************

You might also like