The approach that I am finally taking is to define an interface say FooInterface
with foo()
method and let all the child classes implement it, then I could just type cast the obj to that interface and call foo()
method like this:
Parent obj = ...// Object of one of the child classes
.....
if(obj instanceof FooInterface){
((FooInterface)obj).foo();
}