64

To reduce dependece of class, I want to send parameter (using generic class) to constructor that extends some class and implements interface, for example

public interface SomeInterface{
    public void someMethod();
}

public class MyFragment extends Fragment implements SomeInterface{
    //implementation
}

//here is classs, that I need to create. T must extend Fragment and implements 
//SomeInterface. But, I'm afraid, if I'll use MyFragment directly, it will create a
//dependence of SomeClass from MyFragment.

public class SomeClass /*generic?*/ {
    public SomeClass(T parent);
}

Is it possible?

Futher, using my T class, I want to create views, using T.getActivity() as Context.

1
  • Can you provide more information? Using Java reflection you can pass a Class object into a method and inspect it for its implemented interfaces via getInterfaces(), as well as obtain/call a Constructor to make a new instance. But your specific use case is unclear, so it's difficult to figure out how to piece these items together to solve your particular problem.
    – devunwired
    Jan 15, 2012 at 16:41

5 Answers 5

151

T must extend Fragment and implement SomeInterface

In that case you could declare SomeClass as the following:

public class SomeClass<T extends Fragment & SomeInterface>

That would require an object of type T to both extend Fragment and implement SomeInterface.

Further, using my T class, I want to create views, using T.getActivity() as Context.

I'm unfamiliar with Android, but if getActivity() is a public instance method declared in Fragment then will be entirely possible to call it on an instance of T, since the compiler will know all Ts must inherit that method.

3
  • How would you use Mockito to create a mock class that is of type T?
    – B W
    Aug 14, 2017 at 15:01
  • 1
    @bwicks Good question. I suspect your test class might need to declare a type like abstract class TestFragment extends Fragment implements SomeInterface and then mock that type with TestFragment.class. Aug 15, 2017 at 15:53
  • This is so incredibly unintuitive, thanks. ^^'
    – Panossa
    Feb 11, 2022 at 15:03
6

Do you want to do something like this?

class SomeClass {}

interface SomeInterface{
    public void someMethod();
}

class AGenericClass<T> extends SomeClass implements SomeInterface {
    public void someMethod() {}
}

This is allowed.

But what's Android specific about your question? I think I must be missing something so can you provide some more details?

update below

I'm still not totally sure what you want to do, and I'm also not sure you need to worry about the dependence right away, but this is also legal, using bounded type parameters:

interface SomeInterface{
    public void someMethod();
}

class Fragment {
    public void aMethodInFragment() { }
}

public class SomeClass <T extends Fragment & SomeInterface> {
    public SomeClass(T parent) {
        parent.someMethod();
        parent.aMethodInFragment();
    }
}

The type parameter <T extends Fragment & SomeInterface> specifies that T should be a subclass of Fragment and implement SomeInterface.

0
4

It's easy if i understand you correct you to have some type T which is extending Fragment and implementing some interface. So you have to write something like that.

abstract class TemplateFragment extends Fragment implements SomeInterface{}

and then in your classes

class SomeClass<T extends TemplateFragment> {
...
}

It will do the trick.

1
  • It works but not if your subclasses need to implemented a different kind of "Fragment" for exmaple when instead of Fragment we want to use View here. So subclasses could be a CustomView extends LinearLayout implements SomeInterface Since LinearLayout is a View the condition is still true when using BaseClass<T extends View & SomeInterface>
    – Koen
    Aug 23, 2019 at 9:28
1

For Kotlin you can use such syntax:

class MyClass<T> where T: AnotherClass, T: SomeInterface
0

The Kotlin equivalent would be:

class SomeClass<T>() where T: Fragment, T: SomeInterface

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.