Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34780

Java method to return class subtype (not instance, class itself)

I have a class BaseViewHolder, which is a base class, and a subclass of it, MyViewHolder.

I have a different abstract class, BaseAdapter that requires subclasses to to implement a method that returns a subclass of BaseViewHolder. I've tried:

protected abstract <T extends BaseViewHolder > Class<T> getViewHolderClass();

Then I create a class MyAdapter, which is a subclass of BaseAdapter and I implement the getViewHolderClass method:

@Override
protected <T extends BaseViewHolder > Class<T> getViewHolderClass() {
    return MyViewHolder.class;
}

But I'm getting:

Incompatible types.
Required: Class<T>
Found: Class<path.to.my.package.MyViewHolder>

How can I make getViewHolderClass return a class that is or is derived from BaseViewHolder (without unnecessary casting to more general classes or ?, of course)? The closest I've found on StackOverflow is How do I make the method return type generic? but that involves returning class instances, whereas I'm trying to return a class itself.

Upvotes: 1

Views: 371

Answers (3)

kewne
kewne

Reputation: 2298

The getViewHolder method doesn’t need to be generic, it can return Class<? extends BaseViewHolder>.

The reason the code doesn’t compile is that the return type is always Class<MyViewHolder>, which doesn’t allow calls like adapter.<AnotherViewHolder>getViewHolder(), although the generic method signature implies it does.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311163

You need to specify MyAdapter's T as MyViewHolder:

public class MyAdapter extends BaseAdapter<MyViewHolder> {

    @Override
    protected Class<MyViewHolder> getViewHolderClass() {
        return MyViewHolder.class;
    }

    // Other methods...

Upvotes: 0

FThompson
FThompson

Reputation: 28687

You'll need to use the ? wildcard.

@Override
protected Class<? extends BaseViewHolder> getViewHolderClass() {
    return MyViewHolder.class;
}

Upvotes: 2

Related Questions