• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface variables

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic