I need to write a method which depends on the type we're passing as an argument. For instance:
public class A{
void foo(Object o){
if(o instanceof Integer){
System.out.println("Integer");
}
if(o instanceof Date){
System.out.println("Date");
}
}
//And so forth
}
In my particular case the method has much more complex structure, but it doesn't matter here.
Of course if-then-else
clauses are things that an object oriented program try to eliminate.
I think polymorhism can be help of here. But I have not realized how it can be helpful. Any suggestions?
UPD: I need to perform run-time type checking, therefore method overloading won't be helpful in that case.
UUPD:
I'm going to call the method in the following way:
Object o = null;
//Initializng the object, could be Integer, Date or String
A a = new A();
a.foo(o); //So I need to decide what to do from the run-time type of o
//that's why overloading won't be helpful here.