• 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

same var in parent and child interface

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i am using jdk 1.5 and compiling below code, its not giving as there is ambiguity of variable i.

interface a{
public static final int i=3;
public boolean equals1(Object obj);
}
interface b extends a{
public static final int i=30;
}

now First class is implementing b,
Output of this statement : System.out.println(((b)new First()).i);
is coming as 30
and
output of this: System.out.println(((a)new First()).i);
is coming as 3

I feel that it should show a ambiguity error
[ June 29, 2007: Message edited by: Pushkaraj Thorat ]
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


...this doesn't give problem why should that?
 
Pushkaraj Thorat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not class it should be interface.
and in interface all variables are

public static and final

and how come final variables should be allowed to change...
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be something which confuses lots of people. See this recent thread. Only that was about classes.

If you have a subclass, any variables with the same name hide the variable in the superclass. You haven't altered i = 3, but only hidden it. The 3 is still there, only you can't find it.
Try this sort of thing:-Then see whether this sort of thing will work, or whether it even compiles at all:-

Try that and tell us what happens.
 
Pushkaraj Thorat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm I go it
Thanks
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.

The reason it won't compile is that you have a conflict in diamond inheritance.Different values of i are inherited by the different routes, and the compiler cannot resolve the conflict.
 
Pushkaraj Thorat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But is there any way by which we can access the var 'i' of interface
without creating instance of interface(concrete class) and casting to interface.
[ July 02, 2007: Message edited by: Pushkaraj Thorat ]
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try implementing one interface with an inner class . . .

But it will get you thrown out of the beginners' forum.

Anybody else got any ideas?
 
reply
    Bookmark Topic Watch Topic
  • New Topic