Page 1 of 1

About Question enthuware.ocpjp.v7.2.1770 :

Posted: Fri Jan 29, 2016 3:20 pm
by Sergiy Romankov
" Since Eatable and Food both have a static int field name types"
is it true? Is field types in Eatable static?

Re: About Question enthuware.ocpjp.v7.2.1770 :

Posted: Fri Jan 29, 2016 9:15 pm
by admin
Yes, all fields in an interface are public, static, and final. Even if you do not explicitly declare them so. This is OCA stuff :)
Paul.

Re: About Question enthuware.ocpjp.v7.2.1770 :

Posted: Sat Jun 03, 2017 9:07 am
by Cannelids
The explanation makes sense, and when I alter the code in various ways things change as I would expect, though there is an interesting ramification: if the explicit “implements Eatable” declaration is removed from LINE 1 of Fruit the compile errors disappear. I assume this is because, although the class still inherits the Food ‘true’ and, indirectly, the Eatable ‘true’, the former shadows the latter. (Consistent with this, if LINE 2 is disabled/removed, Line 3 prints the ‘shadowing’ value, 20, and it can be confirmed that Fruit still implements Eatable indirectly through Food by showing that “new Fruit() instanceof Eatable” returns ‘true’.) So while I always assumed that ‘re-declaring’ an interface in a subclass of a class that implements that interface makes no difference, it seems it can affect ‘shadowing relationships’. (Another way to get LINE 2 and 3 to compile is to add an explicit declaration of a static ‘types’ field to Fruit in the original code, so that this third ‘types’ variable now shadows the others.)