I have 3 classes: **Parent**, **Child1**, and **Child2**. Both Child1 & Child 2 extend Parent and they **cannot** be modified.
There is a class Action defined as follows:
public class Action {
public static void perform(Parent p) {
if (p instanceof Child1) {
action((Child1)p);
}
else if (p instanceof Child2) {
action((Child2)p);
}
else {
action(p);
}
}
private static void action(Parent p) {
...
}
private static void action(Child1 c1) {
...
}
private static void action(Child2 c2) {
...
}
}
Parent p = new Child1();
Action.perform(p);
Without instanceof operator, is there any way to get the same behavior as above ?
======
(modified)
PS: the given argument type of Action.perform is Parent, but its value is Child1, so I think method overloading doesn't work ....