Abstract class with fields implementing ValueObject

Dan Smith daniel.smith at oracle.com
Sun Feb 13 18:05:29 UTC 2022


> On Feb 12, 2022, at 10:16 PM, Srikanth Adayapalam <srikanth.adayapalam at oracle.com> wrote:
> 
> I understand Frederic is asking about whether the spec​ inadvertently allows something it should not - Here anyway is javac behavior:
> 
> Given:
> 
> abstract class A implements ValueObject {
>     int x;
> }
> 
> on compile:
> X.java:1: error: The type A attempts to implement the mutually incompatible interfaces ValueObject and IdentityObject
> abstract class A implements ValueObject {
>          ^
> 1 error

Yep, this is expected and consistent: javac sees the field and infers the superinterface IdentityObject (per the language rules), then detects the conflict between interfaces.

A slightly more interesting variation: declare a simple interface Foo; change to 'A implements Foo'. This compiles fine, inferring A implements IdentityObject. Then separately compile Foo so that it extends ValueObject. No compilation error, but the JVM should detect the IdentityObject/ValueObject conflict when A is loaded.

To generate the kind of class files Fred asked about, you'd need to use something other than javac.



More information about the valhalla-spec-observers mailing list