File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface variables" Watch "Interface variables" New topic

Interface variables

Vidya Venkatesh

Joined: Mar 06, 2003
Posts: 12
hi, when I compile this program, compiler complains that the reference to the variable value is ambiguous. Here I2 extends I1, then why doesn't its version of value hide that of I1 ?
class Test implements I1, I2
public void f() { System.out.println(value); }
public static void main(String[] args)
Test t = new Test();
interface I1
int value = 1;
void f();
interface I2 extends I1
int value = 2;
void f();
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
Let me take a stab at this one... Since interface variables are implicitly public static and final.. a static final variable that is initialised cannot be initalised again....Hence the variable in I1 is not hidden by the variable in I2... This implies that both values are available to the class that implements the interfaces I1, I2 , hence the ambiguity
pinky yadav
Ranch Hand

Joined: Jun 17, 2002
Posts: 44
i think I2 hides the variable value but since the class is implementing both the instances there is an ambiguity as both value are available.
If you make the class implement only I2 there wont be an ambiguity.
Mandar Puranik
Ranch Hand

Joined: Jan 08, 2003
Posts: 35
Hi Vidya,
In my humble opinion, I think if we want to implement more than one interface, we have to use extends and not implements.
for example :
if I1 and I2 are two interfaces,
and we want a class to extend both these,
then it should be :

Will this make any difference ?
Please let me know if I am on the right track.

To Bug is Human,<br />To Debug Divine... :-))
Kevin Arnold
Ranch Hand

Joined: Apr 30, 2003
Posts: 30
Originally posted by Vidya Venkatesh:
Here I2 extends I1, then why doesn't its version of value hide that of I1 ?

It's because the class Test declares that it implements both I1 and I2. If I2 extends I1, isn't it enough to say that Test implements I2 ?

[ May 02, 2003: Message edited by: Kevin Arnold ]
I agree. Here's the link:
subject: Interface variables
It's not a secret anymore!