Generic type when removing collection method in Java

For some strange reason, I want to implement a collection from Java Util and with a generic type of everything, including delete and contains, that for backward compatibility reasons they didn't do it at all, so I want to try it myself. Here is the code I want to look at:

public class MyTest<E> implements Collection<E>{

    @Override
    public <T> boolean remove(T t){
        return true;
    }

    @Override
    public <T> boolean contains(T t){
        return true;
    }

}

      

As my research continues, I understand that this code will wear out over time as remove(Object) of type Collection<E>

but java just seems to not accept it instead, but keeps asking to override the method with an Object argument as an argument. So I ask if anyone knows about this or directly with this

+3


source to share


1 answer


You cannot have this implementation Collection

like in Java, arguments cannot use covariance or overridden, but Collection

defines these two methods:

boolean remove(Object o);
boolean contains(Object o);

      



If you want to implement an interface Collection

, you must implement this method as they are specified:

public class MyTest<E> implements Collection<E>{

     ... 
    @Override
    public boolean remove(Object  o){
       . . .
    }

    @Override
    public boolean contains(Object o){
       . . .
    }
     ...     
}

      

+3


source







All Articles