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