Alex Opent
Alex Opent

Reputation: 111

Overriding already overridden methods in derived class

I have a class CarSettingsAdapter that extends FragmentStatePagerAdapter. In that class I have a overridden method that return item of type CarSettings

@Override
    public CarSettings getItem(int position) {
        return carSettingsList.get(position);
    }

Now I creating a new class that extends class CarSettingsAdapter but I want it to return my own type - let say MyTypeB

public class NewCarSettings extends CarSettings {

@Override
    public MyTypeB getItem(int position) {
        return typeBList.get(position);
    }
}

I cant override it, I getting an compile error - The return type is incompatible with CarSettingsAdapter.getItem(int).

I want to use all the logic of CarSettingsAdapter, that's why I extends it, but also to override the basic methods. How can I do it?

Upvotes: 1

Views: 3323

Answers (1)

Quanturium
Quanturium

Reputation: 5696

You could create an intermediate class that uses generics. For example CustomSettingsAdapter<T> extends FragmentStatePagerAdapter.

That way your getItem() method could return T.

Then you put the common logic in that class and create your new classes like this:

  • CarSettingsAdapter extends CustomSettingsAdapter<CarSettings>
  • NewCarSettingsAdapter extends CustomSettingsAdapter<NewCarSettings>

Upvotes: 2

Related Questions