In this example the asString() invocations do not compile. (Aside: Invoking toString() does compile but I need to customize.) How do I invoke asString, or any method, of a generic type, given that the method has in fact been provided for every possible T?
class Range<T>
{
private T min;
private T max;
Range(T min, T max)
{
this.min = min;
this.max = max;
}
String makeString()
{
return "The range is from " + min.asString() + " to " + max.asString();
}
}
You need to provide an interface that has
asStringmethod defined, for example:Then define your class like this: