Ok, so I am creating a dynamic 2D array in Java that implements the java.util.Collection interface. I made my array implementing it, because I wanted it to have the same functionality as normal Collection. However, I cannot implement the method size()because it returns an integer in the interface, and the 2D matrix can potentially overflow the integer type.
Here is a snippet of my class that I am trying to do:
public abstract class AbstractMatrix<E> implements Collection<E>{
@Override
public long size() {
return columns * rows;
}
}
Now this will not work, because "The return type is incompatible with Collection<E>.size()", and if I change the type to int, the column rows * may overflow.
I know I can't override a size method like this, but is there any way to make sure the method returns the correct size while still implementing the interface Collection?
Yes, I know that this is impractical and probably will never be a problem, but I was interested to know if there is a good solution for this.
source
share