Object reference or Copy Constructor using Java



A class that
contains a method which will have parameter of type class itself (object
reference), How to pass value to that method from main.





Program:





class Rectangle


 {


  double l,b;





  Rectangle(double x,double y)


   {


    l=x;


    b=y;


   }


  double RecArea(Rectangle ob)


    {


     return (ob.l*ob.b);


    }


 }


class Sk20


 {


 


  public static void main(String args[ ])


   {


     Rectangle 
ob1= new Rectangle(4,5);


     double ar = ob1.RecArea(ob1);


     System.out.println(+ar);


    }


 }





Compile:


D:\>javac
Sk20.java





Run:


D:\>java Sk20





Output:




20.0


Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C