Covariant overrides on the Buffer Hierachy

Ulf Zibis Ulf.Zibis at
Tue Apr 15 16:56:49 UTC 2014


Am 15.04.2014 10:05, schrieb Richard Warburton:
> Hi,
> I'd like to have a discussion about tidying up a few core library method
> signatures in a way that (I think) is backwards compatible.
> I've been using ByteBuffer quite a lot recently which is designed to be a
> fluent API. Unfortunately its quite inconvenient to use because there's a
> hierarchy of classes at play (MappedByteBuffer, ByteBuffer and Buffer) and
> methods which are inherited from parent classes aren't overridden with
> covariant return types. For example the following code doesn't compile
> because clear is defined on Buffer and putInt is defined on ByteBuffer.
> ByteBuffer buffer = ByteBuffer.allocate(256);
> ByteBuffer other = buffer.duplicate()
>                           .clear()
>                           .putInt(0, 4);
> If clear was overridden in ByteBuffer with ByteBuffer as its return type
> then this would compile.

See also:


More information about the core-libs-dev mailing list