Assume we have a Parent interface with compare() function.
public interface Parent {
public int compare(Parent otherParent);
}
Suppose that children Child1, Child2, Child3 implement this interface Parent
public class Child1 implements Parent {
@Override
public int compare(Parent other) {
Child1 otherChild = (Child1)other;
}
}
Also, I am using generics <T extends Parent>
everywhere else in the code. So I need to compare two objects of type T from other parts of code.
I understand this is a bad design as I am typecasting the Parent object in compare() function and I don't even know whether the input is of type Child1.
How can I avoid this type casting while using generics?