Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic interface in Java

Tags:

java

android

Is it possible to have something like that? I'm trying to force any class extending this one to implement an interface that extends BaseHomeListView

public abstract class BaseHomeFragment<T extends BaseHomeListView> extends BaseRecyclerViewFragment implements T 

I'm trying to implement MVP pattern in Android for some fragments which only display lists. So basically the view has to rendersList, that's why it is in the base interface, however I still want to allow each fragment to have add more methods as they need

public interface BaseHomeListView<T> extends LoadDataView, LoadMoreView<T> {
   void renderList(Collection<T> items);
}
like image 647
atabouraya Avatar asked Jan 28 '26 03:01

atabouraya


1 Answers

The only sensible thing you can do is the following:

public abstract class BaseHomeFragment<T> 
    extends BaseRecyclerViewFragment 
    implements BaseHomeListView<T>

And then if you have something like

public interface FancyHomeListView extends BaseHomeListView<Fancy> {
}

Then you can just have a fragment like

public class FancyHomeFragment 
    extends BaseHomeFragment<Fancy>
    implements FancyHomeListView {
    //...
}
like image 90
EpicPandaForce Avatar answered Jan 29 '26 15:01

EpicPandaForce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!