java overloaded method

Posted by Sean Nguyen on Stack Overflow See other posts from Stack Overflow or by Sean Nguyen
Published on 2011-01-09T18:03:01Z Indexed on 2011/01/09 18:53 UTC
Read the original article Hit count: 180

Filed under:

Hi,

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,

© Stack Overflow or respective owner

Related posts about java