Anybody can help me to identify the object?
Here I have two object, one is the instance of the parent class Class1, another is the instance of child class Class2, how to identify them in my program? Thanks in advance.
Here I attach the logic in my program:
public void myfunction(Object obj)
{
if (obj instanceof Class1)
process1();
if (obj instanceof Class2)
process2();
}
when I invoke myfunction with parameter obj which is the instance of Class2, the process1 was run, and process2 was run too, why it happens? how to identify them?
[562 byte] By [
lynn_lee] at [2007-9-30 8:31:47]

I'm a real newbie with Java, but have you considered looking at something like "getClassName()"
String name=obj.getClassName();
if (name == "Class1") process1();
if (name == "Class2") process2();
...
I think the problem that your having is that Class2 is a subclass of Class1, so anything which is an instance of Class2 is implicitly also an instance of Class1. But I could be wrong - I am a newbie after all.
Do you mean you only want only process to be run? If so, change you code to:
public void myfunction(Object obj)
{
if (obj instanceof Class1)
process1();
if (obj instanceof Class2)
process2();
}
If the obj is a member of both classes, both will be run.
That should have been
public void myfunction(Object obj)
{
if (obj instanceof Class1)
process1();
else if (obj instanceof Class2)
process2();
else
System.err.println("Unexpected object type: "+obj);
}
HI, John_McKown , Thanks for your reply, I think the solution from you is a good solution, but for the abstract parameter obj, I can not know the class name in advance. Maybe I have got another problem.
Hi, Peter-Lawrey , you bring me to another problem, if the obj is grand-child or grand-grand-child of these two classes, myfunction can not be run correctly, it means the function myfunction is a bad function, I will look for another solution for this case. Thank you for your reminder.
Such problems are best handled with virtual functions.
> That should have been
> > public void myfunction(Object obj)
> {
> if (obj instanceof Class1)
> process1();
> else if (obj instanceof Class2)
> process2();
> else
> System.err.println("Unexpected object type: "+obj);
> }
>
>
Since Class1 is the superclass of Class2, every instance of Class2 is an instance of Class1, and process2 will never be executed in the above code.
You should probably use a callback method. E.g:
public class MyClass {
public final void process1() {
System.out.println("process1 called");
}
public final void process2() {
System.out.println("process2 called");
}
public void mymethod(Processable p) {
p.process(this);
}
public static void main(String args[]) {
MyClass instance = new MyClass();
instance.mymethod(new Class1());
instance.mymethod(new Class2());
}
}
interface Processable {
void process(MyClass processor);
}
class Class1 implements Processable {
public void process(MyClass processor) {
processor.process1();
}
}
class Class2 extends Class1 {
public void process(MyClass processor) {
processor.process2();
}
}
Note that the mymethod method does not need to be updated every time you add a new subclass - you just need to write a process method for the new subclass.
Dear mattbunch, thanks for your reply, I think you are the expert.