Java2all.com  

More about method





Share This Topic -

1. Passing Objects as a Parameter to Method.

We have seen that methods can take parameters as input and process them.

It is also common to pass objects as a parameter to methods.

 class PassObj
{
        int n1;
        int n2;
        // constructor
        PassObj()
        {
                n1 = 0;
                n2 = 0;
        }

        PassObj(int p1, int p2)
        {
                n1 = p1;
                n2 = p2;
        }

        void multiply(PassObj p1)
        {
                int temp;
                temp = p1.n1 * p1.n2;
                System.out.println("Multiplication is " + temp);
        }
        public static void main(String args[])
        {
                PassObj obj1 = new PassObj(5,6);
                PassObj obj2 = new PassObj();
                obj2.multiply(obj1);
        }
}

 
Output :
 
Multiplication is 30
 
2. Method overloading with object as a parameter.
 class MetObjOv
{
        int n1;
        int n2;

        // constructor
        MetObjOv()
        {
                n1 = 0;
                n2 = 0;
        }
        MetObjOv(int x, int y)
        {
                n1 = x;
                n2 = y;
        }
        void multiply(MetObjOv p1)
        {
                n1 = p1.n1;
                n2 = p1.n2;
                System.out.println("There is nothing to multiply ");
                System.out.println("n1 = "+n1+"\tn2 = " +n2);
        }
        void multiply(MetObjOv p1, MetObjOv p2)
        {
                n1 = p1.n1 * p2.n1;
                n2 = p1.n2 * p2.n2;
                
                System.out.println("Multiplication of two objects ");
                System.out.println("n1 = " + n1 + "\tn2 = " + n2 );
        }

        public static void main(String args[])
        {
                MetObjOv obj1 = new MetObjOv(5,6);
                MetObjOv obj2 = new MetObjOv(6,5);
                MetObjOv obj3 = new MetObjOv();
                obj3.multiply(obj1);
                obj3.multiply(obj1, obj2);
        }
}

 
Output :
 
There is nothing to multiply 
n1 = 5 n2 = 6
Multiplication of two objects 
n1 = 30 n2 = 30
 
3. Return an Object.
 
A method can return any type of data, including class type (object) that you create.
 class RetObj
{
        int n1;
        int n2;

        // constructor
        RetObj()
        {
                n1 = 0;
                n2 = 0;
        }
        RetObj(int x, int y)
        {
                n1 = x;
                n2 = y;
        }
        RetObj multiply(RetObj p1, RetObj p2)
        {
                n1 = p1.n1 * p2.n1;
                n2 = p1.n2 * p2.n2;
                return (this); 
        }

        void display()
        {
                System.out.println("An Example of returning an Object ");
                System.out.println("n1 = "+n1+"\tn2 = " +n2);
        }
                
        public static void main(String args[])
        {
                RetObj obj1 = new RetObj(5,6);
                RetObj obj2 = new RetObj(6,5);
                RetObj obj3 = new RetObj();
                obj3 = obj3.multiply(obj1, obj2);
                obj3.display();
        }
}

 
Output :
 
An Example of returning an Object 
n1 = 30 n2 = 30
 
RetObj multiply(RetObj p1, RetObj p2) This is the syntax in our program which has return type object.
 
obj3 = obj3.multiply(obj1, obj2); this is the syntax which calls method multiply and return object, it will store in obj3.