Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

K. K.

Wagh Polytechnic, Nashik-3

Chapter 2

Derived Syntactical Constructs in


Java
Constructor
• A constructor initializes an object immediately upon creation.
• It has the same name as the class in which it resides and it is
syntactically similar to a method.
• Once defined, the constructor is automatically called immediately
after the object is created, before the new operator completes.
• Constructors look a little strange because they have no return
• type, not even void.
• This is because the implicit return type of a class’ constructor is
the class type itself. It is the constructor’s job to initialize the
internal state of an object.
• By doing this the code creating an instance will have a fully
initialized, usable object instantly.
Types of Constructor
• Default Constructor:
It is a constructor with no arguments passed to it.
• Parameterized Constructor:
It is a constructor with parameters or arguments.
• Sample programs
• Constructor Overloading:
Usually in this more than one type of constructor is used in same
program.
Sample Program: Default Constructor
class Box
{ int height, depth, length;
Box()
{
height = depth = length = 10;
}
void volume()
{
int vol=height* depth * length;
System.out. Println(“Volume is:”+vol);
}
}
class BoxClass
{
public static void main(String args[])
{
Box a = new Box(); //statement1
a. volume();
}
}
Sample Program: Parameterized Constructor
class Box
{ int height, depth, length;
Box(int H, int D, int L)
{
height=H; depth=D; length=L;
}
void volume()
{
int vol=height* depth * length;
System.out. Println(“Volume is:”+vol);
}
}
class BoxClass
{
public static void main(String args[])
{
Box a = new Box(10,10,10); //statement1
a. volume();
}
}
Sample Program Constructor Overloading
class Box
{
int height, depth, length;
Box()
{
height = depth = length = 10;
}
Box(int x,int y)
{
height = x; depth = y;
}
Box(int x, int y, int z)
{
height = x; depth = y; length = z;
}
}
Sample Program Cont.

class BoxClass
{
public static void main(String args[])
{
Box a = new Box(); //statement1
System.out.println("depth of a : "+a.depth);
Box b = new Box(12,23); //statement2
System.out.println("depth of b : "+b.depth);
Box c = new Box(99,84,36); //statement3
System.out.println("depth of c : "+c.depth);
}
}
this Keyword

• ‘this’ is always a reference to the object of the current


class’ type.
• Uses:
1) It prevents hiding of instance variables due to same
name given to the parameters passed in Constructor or
Method.
2) Calling the constructor
this Keyword Cont. Sample Program for use 1
class Box
{ int w,h,d;
Box(int w, int h, int d)
{ this.w=w; this.h=h; this.d=d;
}
void vol()
{ int v=this.w * this.h * this.d;
System.out.println(“Volume of a Box is:”+v); }
}
class TK
{ public static void main(String ar[])
{ Box b1 = new Box(10, 10, 10);
b1.vol(); }
}
Output Volume of a Box is: 1000
this Keyword Cont. Sample Program for use 2
class Box
{ int w,h,d;
Box() //Constructor 1
{ w=10; h=0; d=0; }
Box(int H) //Constructor 2
{ this(); //Calling Constructor1
h=H;
}
Box(int H, int D) //Constructor 3
{ this(H); //Calling Constructor 2
d=D;
}
void vol()
{ int v=w * h * d;
System.out.println(“Volume of a Box is:”+v);
} }
this Keyword Cont. Sample Program for use 2 Cont.
class TK1
{
public static void main(String ar[])
{
Box b1 = new Box(20, 30);
b1.vol(); }
}
Output Volume of a Box is: 6000

You might also like