Java - 5.1 Constructors PDF
Java - 5.1 Constructors PDF
Java - 5.1 Constructors PDF
3
DEFINITIONS
4
DEFAULT CONSTRUCTOR
Its of 2 type
User Provided by
Defined the system
5
PARAMETERIZED CONSTRUCTOR
When a constructor takes in arguments it is known as
parameterized constructor
The actual values are passed during object creation
9
THIS KEYWORD
this keyword in Java is a special keyword which can be
used to represent current object or instance of any class in
Java.
It stores the address of current calling object
It is also known as reference or pointer to current object.
Any instance method can be called as this.method() and
any instance variable var as this.var.
Methods Purpose
Bill() constructor to initialize data members with default initial value
Bill(...) parameterised constructor to accept billno, name and no. of calls consumed
Calculate() to calculate the monthly telephone bill for a consumer as per the table
Display() to display the details
Units consumed Rate
First 100 calls ₹0.60 / call
Next 100 calls ₹0.80 / call
Next 100 calls ₹1.20 / call
Above 300 calls ₹1.50 / call
21
Fixed monthly rental applicable to all consumers: ₹125
Create an object in the main() method and invoke the above functions to
perform the desired task.
FILL IN THE BLANKS
Question 1
A member function having the same name as that of the class name is
called constructor.
Question 2
A constructor has return no type.
Question 3
A constructor is used when an object is created
Question 4
A constructor without any argument is known as non-parameterised
constructor.
Question 5
Parameterised constructor creates objects by passing value to it.
Question 6
The this keyword refers to the current object.
Question 7
The argument of a copy constructor is always passed by reference.
Question 8
The constructor generated by the compiler is known as default constructor. 22
TRUE/FALSE
Question 1
The compiler supplies a special constructor in a class that does not have any constructor.
True
Question 2
A constructor is not defined with any return type.
True
Question 3
Every class must have all types of constructors.
False
Question 4
A constructor is a member function of a class.
True
Question 5
Constructor is used to initialize the data members of a class.
True
Question 6
A constructor may have different name than the class name.
False
Question 7
A constructor is likely to be defined after the class declaration.
False
Question 8
Copy constructor copies functions from one object to another.
False
23
24