//Demonstration Of Constructor OverLoading
public class Rect {
int l,b;
Rect() //Constructor 1
{
l=10;
b=5;
}
Rect(int l,int b) //Constructor 2
{
this.b=b;
this.l=l;
}
int area()
{
return l*b;
}
public static void main(String args[])
{
Rect ob1=new Rect();
Rect ob2=new Rect(15,10);
System.out.println("Ob 1 Area : "+ob1.area());
System.out.println("Ob 2 Area : "+ob2.area());
System.out.println();
}
}
public class Rect {
int l,b;
Rect() //Constructor 1
{
l=10;
b=5;
}
Rect(int l,int b) //Constructor 2
{
this.b=b;
this.l=l;
}
int area()
{
return l*b;
}
public static void main(String args[])
{
Rect ob1=new Rect();
Rect ob2=new Rect(15,10);
System.out.println("Ob 1 Area : "+ob1.area());
System.out.println("Ob 2 Area : "+ob2.area());
System.out.println();
}
}
No comments:
Post a Comment