I have the following methods in one class:
void delete(ClassA a) {
//....
}
void delete(ClassB b) {
//...
}
void delete(List<ClassA> list) {
for (ClassA a: list) {
delete(a);
}
}
void delete(List<ClassB> list) {
for (ClassB b: list){
delete(b);
}
}
The overloading works fine for the first two methods. But there's a conflict between third and fourth methods.
Is it possible to replace them with one method? I tried the following method, which is not correct:
void delete(List<? extends Object> list){
for (Object o: list){
delete((o.getClass())o);
}
}
How can I detect the class type of the instance in list at runtime and call the appropriate method?
instanceofto check object