Professional Documents
Culture Documents
4
4
4
A constructor is that unit ofa class which is used to initialise the data members. The name of th
is same as the class name. A constructor automatically gets invoked whenever an object of the class is crea
2. General format of defining a constructor.
c1assName( )
{
statements;
}
3. Why is a constructor
{
private int c;
MyClass( )
{
c=IO;
}
7. Parameterized
Constructor.
object when it is created.
8. Example of parameterized
class MyClass2
These constructors
Constructor.
{
int c;
public MyClass2(int
{
c=num;
}
num)
}
9. Creating an object in the default
class CreateObject
{
public static void maine )
60
'7~
constructor.
have arguments.
are pas
{
MyClass ob=new MyClass();
IC
II Here, MyClass2 is the name of another class and ob its new objects.
}
}
Formal parameters.
The arguments in the first line of definition of the constructor are called formal parameters. Within
'he constructor definition, the values of these formal parameters (or formal variables) are used to initialise the private
ata members.
Actual parameters.
The arguments passed to the constructor function from the object are called actual parameters. The
alues of actual parameters get copied to the formal parameters.
Example of formal parameters.
lass Numbers
int a, b;
Numbers (int x, int y)
{
a= x;
b = y;
}
the above example, x and yare formal parameters
Example of Actual parameter .
class AnotherClass
I*Constructor
I*1
I*Constructor2
*I
b=num2;
}
Example()
{
Constructors
a=O;
b=O;
}
public static void main()
{
J*calls constructor2*J
'* cans
J*calls
!*End
J*End
}
}
17. Destructor.
destructor's
A destructor is a member method that gets invoked when the object scope ends. Remember that concept
is limited to language C++. Java supports destruction of objects through its garbage collection feature.
SECTION-A
[2011)
overloading
an example.
Ans.
with
(2011)
Constructor
Overloading.
Defining more than one
constructor
for a class where they differ in the
parameter types or number of parameters passed to
the constructor is called constructor overloading. In
this case, the constructors
get called automatically
4.
(a) Differentiate
between
Actual Parameter.
Or
Write the difference between formal parameters
actual parameters.
Give example to illustrate.
(b) Why do we need
member?
a constructor
and
as a class
(2007)
@3J 7~
~~
constructor 3 *'
constructor I*J
of main*!
of class*!
Actual parameters
are the list of parameters
specified
in the statement
that calls th
function. These are the variables that actually sto
the value which is passed to the formal paramete _
and then used up in the function definition.
Separate memory locations are created for act
parameters and formal parameters. The valu _
stored in actual parameters get copied to form
parameters
and then used in the body of t _
constructor method.
For example:
void fun(float x, int y) {
J* x and yare formal
parameters
System.out.println(x);
int d= y+IO;
System.out.println( d);
}
void abet)
{ int a=5;float b=lS;
J* b and a are actual parameters whose
values ae passed in x & y respectively *
fun(b,a); }
(b) A constructor is used to initialize values to the
data members of the class.
For example: class Abc {
short x;
int y;
Abc() {
J* a non-parameterized constructor * J
x=I;
y=234;
}
Ans.
class Swap{
inta, b;
Swap(int x, int y){
it gets created.
Example of Default Constructor
in MyClass.
. class MyClass
{
private int c;
I*Declaring member variable c*1
MyClass( )
I*Defining a default constructor*1
{
c=IO;
}
}
Enter any two variables
through
constructor
parameters and write a program to swap and print
the values.
[20051
System.out.println("a="
+a);
System.out.printin("b
=" +b);}
public static void maine )
{
int u = 50, v = 100;
Swap ob = new Swap(u, v);
System.out.println("Original
Values :");
ob.display( );
System.out.println
("Swapped Values :");
ob.method'Swapf);
ob.displayt);
}
II end of main
II end of class
ICE nUESTUlNS
6.
Ans.
7.
Ans.
8.
Ans.
9.
Ans.
Constructors
("
e ou
System.out.println("1t
x, int y)
Ans.
(a)
Errorl
Correction
Error2
Correction
Error3
Correction
Errorl
Correction
*1
Error2
Correction
Error3
{
a=x; b=y; c=z;
Correction
xU)
{
Example obj=new Example(5,
I*lnvoking
6, 2.5);
*1
Constructor2*1
}
12. Point
the errors
and
make
corrections
in the
x=='A';
b==truej
7~
: checkerror();
: CheckError()
II name of constructor
: x=='A';
: x='A';
: b==true;
: b=true;
Constructor3*1
5.29);
}
}
I*Constructor2*1
}
public static void main(String
CheckConst
objt
=New CheckConst(false,
[a=x; b=y.}
Example(int
x]
(b)
I*Constructorl
is"+a);
a destructor
in our class.
What is a destructor's
name?
A destructor's name is same as that of a class name
What is the returning type of a destructor?
A destructor does not return any value so it has no
returning type not even void just as constructors.
16. Differentiate
a constructor
from a destructor.
Ans. A constructor gets invoked when an object is created
while a destructor gets invoked when the object is no
longer needed, i. e., the scope of the object comes to
14.
Ans.
15.
Ans.
an end.
defined.
(it) What value will the variable a store when
is defined.
Name the formal parameters in the code.
private int x, Yj
public total;
public Sum(int a, int b)
{
x=a;
y=b;
total = x+y;
}
public static void main
(String x] J)
{
Sum.
(iil) Can you access variables x and y in its
Constructors
(j)
-Abcr ){
System.out.println("Object
}
Destroyed ");
of a constructor.
of Constructor
of a destructor.
SECTION-B
of destructor:
c ass
c
static int j, i;
Abc(){ System.out.println
(" Abc object is now created ");
}
Abc(int a){
i=a;
j*=i;}
Characteristics
Ans. Characteristics
-Account(){ }
(c) Date dt=new Date(20,08,2000);
Date dt=new Dater);
Ans. (a) If a Time class contains the constructor defined
as Time(){
}, the constructor gets invoked on
creation of an object but does nothing while, the
second constructor when invoked would initial ise
data members a and b to value zero.
(b) The first statement is a constructor
of class
Account,
that initialises
instance variables
i and j to 0, while the second statement is a
destructor of class Account that automatically
deinitialises all instance variables of all objects.
(c) The first statement
creates an object dt by
invoking its parameterized constructor. The object
gets initialized with values 20,8 and 2000. While
the second statement creates an object and calls
the default constructor.
US YEARS' aUESTI[]NS
Write a class with name employee and basic as its
data member, to find the gross pay of an employee
for the following allowances and deduction. Use
meaningful variables.
Dearness Allowance = 25% of Basic Pay
House Rent Allowance = 15% of Basic Pay
Provident Fund = 8.33% of Basic Pay
Net pay = Basic Pay + Dearness Allowance +
House Rent Allowance
the
_
Gross Pay = Net Pay - Provident Fund.
[20051
double basic;
II Data Member
II Constructor to initialise basic salary
employee (double b)
Constructors
!!!!!!!!!!!!!!
--