• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface variables

 
Vidya Venkatesh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
Thanks
Vidya
class Test implements I1, I2
{
public void f() { System.out.println(value); }
public static void main(String[] args)
{
Test t = new Test();
t.f();
}
}
interface I1
{
int value = 1;
void f();
}
interface I2 extends I1
{
int value = 2;
void f();
}
 
preeti khane
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Pinky
 
Mandar Puranik
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
Mandar
 
Kevin Arnold
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

Kevin
[ May 02, 2003: Message edited by: Kevin Arnold ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic