I have an abstract template method:
class abstract MyTemplate
{
public void something(Object obj)
{
doSomething(obj)
}
protected void doSomething(Object obj);
}
class MyImpl extends MyTemplate
{
protected void doSomething(Object obj)
{
System.out.println("i am dealing with generic object");
}
protected void doSomething(String str)
{
System.out.println("I am dealing with string");
}
}
public static void main(String[] args)
{
MyImpl impl = new MyImpl();
impl.something("abc"); // --> this return "i am dealing with generic object"
}
How can I print "I am dealing with string" w/o using instanceof in doSomething(Object obj)?
Thanks,
Well you really can't do it. Java can't do double dispatch out of the box. The problem is that the binding of the method calls is usually done mostly at compile time.
Here http://en.wikipedia.org/wiki/Double_dispatch
and here
http://www.javaperformancetuning.com/articles/ddispatch2.shtml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With