Casting objects across parent classes.

I have two classes named PolicyType under different packages

Both are bascially the same but belong to differnet parent Classes.

I have an abstract class method which processes these classes( bascally some set operations)

I want to call this method from a class extending this abstract class.

However the method in the abstract class dosent know which class it needs to use.

My method looks like

public void method ( Object obj){

}

I can get the right class from the object by using obj.getClass();

but then I want to cast obj to the right class and use the object based on the class which called it.

Could anyone point me as to how i can do it?

I want to basically do somethig like this

public void method ( Object obj){

Class cls= obj.getclass();

cls obj1 = (cls) obj;

}

Thanks in advance

[894 byte] By [soner.nouri@insurity.coma] at [2007-11-27 3:37:43]
# 1

Wild guess: I would define a common interface and have the two PolicyType classes implement it. Does that make sense?

demo:

interface PolicyType {

public void setScope(String etc);

}

class A {}

class PolicyTypeA extends A implements PolicyType {

public void setScope(String etc) {

System.out.println("PolicyTypeA.setScope");

}

}

class B {}

class PolicyTypeB extends B implements PolicyType {

public void setScope(String etc) {

System.out.println("PolicyTypeB.setScope");

}

}

public class InterfaceExample {

static void f(PolicyType policy) {

policy.setScope("etc");

}

public static void main(String[] args) {

f(new PolicyTypeA());

f(new PolicyTypeB());

}

}

DrLaszloJamfa at 2007-7-12 8:41:00 > top of Java-index,Java Essentials,Java Programming...