Tuesday 29 October 2013

PARAMETERIZED CONSTRUCTOR

Parameterized constructor is used to assign different values to different object of given class.

Program:-
class Rectangle
{
double width;
double height;
Rectangle(double w,double h)
{
width=w;
height=h;
}
double area()
{
return width*height;
}
}
public class RectangleDemo
{
public static void main(String args[])
{
Rectangle rec1=new Rectangle(10,20);
Rectangle rec2=new Rectangle(3,6);
double area;
area=rec1.area();
System.out.println("area is"+area);
area=rec2.area();
System.out.println("area is"+area);
}
}

Output:-
area is 200.0
area is 18.0

No comments:

Post a Comment