Java - Inheriting from a class that accepts generics?

How do you inherit this class? The following code is broken, but it shows what I'm trying to achieve.

Associated mismatch: type T is not a valid substitution for a restricted parameter of type ActivityInstrumentationTestCase2

public class MyClass<T> extends ActivityInstrumentationTestCase2<T> { public MyClass (Class<T> clazz) { super(clazz); } } 
+4
source share
1 answer

What is the definition of the class ActivityInstrumentationTestCase2?

Most likely, ActivityInstrumentationTestCase2 is defined as:

 class ActivityInstrumentationTestCase2<T extends SomeObject> { ... } 

In this case, the parameters of your object should remain within their parameters of the superclass.

For instance:

 public class MyObject<T extends SomeObject> extends ActivityInstrumentationTestCase2<T> { ... } 

You can also make the parameter narrower. So, if MySomeObject extended SomeObject, you can also say:

 public class MyObject<T extends MySomeObject> extends ActivityInstrumentationTestCase2<T> { ... } 

Edit to add: I just found this one . I assume that you are using the same one, so it needs to be defined as:

 public class MyObject<T extends android.app.Activity> extends ActivityInstrumentationTestCase2<T> { ... } 
+5
source

All Articles