Suppose we have a parent interface with the compare () function.
public interface Parent { public int compare(Parent otherParent); }
Suppose that children Child1, Child2, Child3 implement this Parent interface
public class Child1 implements Parent { @Override public int compare(Parent other) { Child1 otherChild = (Child1)other; } }
In addition, I use generics <T extends Parent> everywhere in the code. So I need to compare two objects of type T with other parts of the code.
I understand that this is a bad design, because I am typing the Parent object in the compare () function, and I don’t even know if the input is of type Child1.
How to avoid this type of exception when using generics?
java generics polymorphism casting
Ajay gupta
source share