Programs on constructor

in this core java tutorial we are learn about constructor .
in java constructor is used to initialized the object state like method, and also contains collection of statement(instruction) , constructor is execute at a time of object creation.
constructor is used to assign a value to the class variable , at a same time of object is created . 
constructor is create by a programmer either it create by it self 

Code :

 

//Program to illustrate the use of Constructor

 

class Rectangle

 

{

 

int len,bre;

 

Rectangle()

{

 

len = 25;

 

bre = 30;

 

}

Rectangle(int l,int b)

 

{

 

len=l;

 

bre=b;

}

 

int calculate()

 

{

 

return(len*bre);

 

}

}

 

class rect

 

{

 

public static void main(String []args)

{

 

int area;

 

Rectangle r1=new Rectangle();

 

Rectangle r=new Rectangle(5,6);

 

area=r.calculate();

 

SUB:-CORE JAVA ROLL NO:-1951012


SYBSC[COMPUTER SCIENCE]

 

 

 

System.out.println("Area:"+area); area=r1.calculate(); System.out.println("Area:"+area);

 

}

 

}

 

Output : 

constructor


Post a Comment

0 Comments