javaandroidtype-erasureerasurename-clash

Name clash because of same erasure


I have an interface like the following

public interface IDrawerItem<T, VH extends RecyclerView.ViewHolder> extends IItem<T, VH>, IExpandable<T, IDrawerItem>, ISubItem<IDrawerItem, IDrawerItem> {
    void bindView(VH holder, List payloads);

}

Im getting the following error

Error:(44, 10) error: name clash: bindView(VH#1,List) in IDrawerItem and bindView(VH#2,List) in IItem have the same erasure, yet neither overrides the other where VH#1,VH#2 are type-variables: VH#1 extends ViewHolder declared in interface IDrawerItem VH#2 extends ViewHolder declared in interface IItem

Why is that so?


Solution

  • Short answer: Because the signature cannot be changed. Extending an interface just adds more signatures to the interface.

    Overriding can only be done in classes, not interfaces. You can not update the signature, but you can make a different implementation of that method using the same signature.

    Interfaces just show a signature which have to be available in the class implementing it. For this reason you can change (override) the implementation in a subclass. The method signature (name and parameters) stay the same, so it still complies with the interface. There is no implementation to override in an interface, hence it shows only thát there is such method in any class implementing this interface, and that it is callable from the instantiated object.