Copy Constructor using Java
Write a program in Java to implement copy Constructor
Program:
class
Rectangle
{
float length,breadth;
Rectangle(float a,float b)
{
length=a;
breadth=b;
}
Rectangle(Rectangle obj)
{
length=obj.length+5;
breadth=obj.breadth+5;
}
float area()
{
return (length*breadth);
}
}
class Sk06
{
public static void main(String args[])
{
Rectangle ob1=new Rectangle(5.5f,6.5f);
float ar1=ob1.area();
System.out.println("Area from
Parameter = "+ar1);
Rectangle ob2=new Rectangle(ob1);
float ar2=ob2.area();
System.out.println("Area from
Object="+ar2);
}
}
Compile:
D:\>javac
Sk06.java
Run:
D:\>java
Sk06
Output:
Area from
Parameter = 35.75
Area from
Object = 120.75
Comments
Post a Comment