I want to know the answer as soon as possible
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   shivakumar_kn
Posted On:   Friday, July 13, 2001 03:06 AM

I have three class example A,B and C. Both Class A and B invoking the Class C.
In Class C i would like to know which class(i.e either A or B) is invoking dynamically.

Re: I want to know the answer as soon as possible

Posted By:   Dermot_Hennessy  
Posted On:   Friday, July 13, 2001 06:09 AM

Have A and B implement a common interface TheInterface. Pass this in your method calls to C, where the method expects a TheInterface. Calling getClass() on TheInterface instance will return the type A or B.


Some code


//interface TheInterface
package test;

public interface TheInterface
{

}

//------------------------------

//class A
package test;

public class A implements TheInterface
{

public void doSomething()
{
C theC = new C();
theC.doIt(this);
}
}

// -----------------------
//class B
package test;

public class B implements TheInterface
{

public void doSomething()
{
C theC = new C();
theC.doIt(this);
}
}

//------------------------
// class C
package test;

public class C
{

public void doIt(TheInterface tt)
{
System.out.println(
"The class is " +
tt.getClass());
}
}

//---------------------------
// class Test
package test;

public class Test
{

public static void main(String[] args)
{
System.out.println(
"Testing A...");
A theA = new A();
theA.doSomething();

System.out.println(
"Testing B...");
B theB = new B();
theB.doSomething();
}
}


Hope this helps.

Dermot
About | Sitemap | Contact