I just recently started messing around with Generics in Java, and am running in to some odd behavior. This is the simplified version, but I have a base class that is extended by multiple classes that gets passed into a generic function. Inside this function, I call a method that has several versions that take different base or derived classes as parameters.
If I have the following:
public class A{};
public class B extends A{};
public class C extends A{};
public class Runner
{
public static void Run( ClassA a ){Do Something};
public static void Run( ClassB b ){Do Something};
public static void Run( ClassC c ){Do Something};
}
void SomeRandomCall<B extends ClassA>( B b )
{
Runner.Run( b );
}
SomeRandomCall<ClassB>( new ClassB() );
I am finding in debug that Runner.Run
is calling the Run( ClassA a )
instead of the Run( ClassB b )
function. Given the two functions, shouldn't Run( ClassB b )
be called since the type specific function is provided?
If this isn't the case how would I be able to have a Generic function that would call be able to call functions that have signatures that take base and derived classes?