Search code examples
javatemplatesgenericsinheritancetype-parameter

In Java, can I inherit a class supplied by a type parameter?


In java, can I do something -more or less- like this? and how?

public class SomeGenericClass<T> extends T{
}

Solution

  • I think this is the relevant quote from the JLS (§8.1.4):

    The ClassType [provided in the extends clause] must name an accessible (§6.6) class type, or a compile-time error occurs.

    (The bit about accessibility isn't relevant).

    A class type is not the same thing as a type variable (§4.3) (which T is), so attempting to do this would be a compile-time error.