For some weird reason, I want to implement a Collection from Java Util and with a generic type of everything, includes remove and contains, which for backward compatibilities reasons, they didn't do it at all, so I want to try myself. Here is what the code I want to look like:
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 going on, I understand that this code eventually going to have the erasure like remove(Object) of type Collection<E>
but java simply seems like not accepting it instead but keep asking to override the method with Object as argument. So I'm asking if anyone knows anyway to work around it or directly with this
You cannot have this implementation of Collection as in Java the arguments cannot use covariance or be redefined and Collection defines these two methods like that :
boolean remove(Object o);
boolean contains(Object o);
If you want to implement the Collection interface, you should implement these method as these are specified :
public class MyTest<E> implements Collection<E>{
...
@Override
public boolean remove(Object o){
. . .
}
@Override
public boolean contains(Object o){
. . .
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With