0
4.7kviews
Write a program to display the area of square and rectangle using the concept of overloaded constructor.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10M

Year: May 2016

1 Answer
0
233views

We declare three methods of same name but with different number of arguments or with different data types. Now when we call these methods using objects, corresponding methods will be called as per the number of arguments or their datatypes.

class Shape
{
static int l,b,S,R;
static void area(int m)
{                                                                                                  //For Square
l=m;     
}
static void area(int x,int y)
{
l=x;                                                                                            //For Rectangle
b=y;
}
void cal()
{
S=l*l;
R=l*b;
}
void display()
{
System.out.println("Area of Square="+S);                       //For Display
System.out.println("Area of Rectangle="+R);
}
public static void main(String args[])
{
Shape s1=new Shape();                                                       //Object s1 of class Shape is created.
area(10);
area(10,2);                                                                             // area() is called .
s1.cal();
s1.display();
}
}

output:

Area of Square=100

Area of Rectangle=20

Please log in to add an answer.