Interface variables
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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();
}
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();
}
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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

If you make the class implement only I2 there wont be an ambiguity.
Pinky

posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
To Bug is Human,<br />To Debug Divine... :-))
posted 22 years ago
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 ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
| You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









