Can we share object as argument object to any method

0

/* Can we share object as argument object to any method.
Ans: yes, we must defined the method with parameter with that object class type. For example A class object we can share as a current to only A class methods. If can be shared as argument objects to either A class or B class */


class A
{
void m1()
{
System.out.println("class A and method m1");
}
void m2(A a)
{
System.out.println("class A and method m2");
}
}
class B
{
void m3()
{
System.out.println("class B and method m3");
}
void m4(A a)
{
System.out.println("class B and method m4");
}
}

class Test
{
public static void main(String[] args)
{
A a1=new A();
A a2=new A();

a1.m1();
a1.m2(a2);

// a1.m3(); //error: cannot find symbol a1.m3();
// a1.m4(a2);  //error: cannot find symbol a1.m4(a2);

B b1=new B();

b1.m3();
b1.m4(a2);
}
}

Compilation:
G:\netlojava>javac Test.java

Execution:
G:\netlojava>java Test

Out Put:
class A and method m1
class A and method m2
class B and method m3
class B and method m4

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts