Forcing type parameter to implement specific method in java generics
generics, java
Solution
You could make an abstract base class called `ItemBase`, make equals abstract and then have `Item extend ItemBase`.
public abstract class ItemBase {
@Override
public abstract boolean equals(Object o);
}
public class Bag extends ItemBase
This would force anyone Implementing ItemBase to specifically implement equals
Problem
Is there a way to force type parameter in java generics to implement `equals` method? For example, I wrote this class: `public class Bag<Item> implements Iterable<Item>` which has `contains` method, that uses `Item.equals` method. So I want to make sure that the passed Object in the generics will also implement the `equals` method.