• 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

why not interface variable??

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
Since the variable declared in the interface are implicitly static and final I thing in the code given below.
<code>
interface Ques01 {String str="Ques01";}
class Ques02 {
String str="Ques03";
}
class Ques extends Ques02 implements Ques01 {
public static void main(String args []){
System.out.println(str);
}
}
</code>
when we execute the main method JVM should select the static variable of the interface(Ques01).As we are not using any reference of the class Ques02 in the static context(main method).
 
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 Harvinder,
This will give a compiler error.
The reason is that both, the class which you extend as well
as the interface you implement, has the same variable str.
So it doesnt come to know which str you want to refer to in your class
ie in Ques.
What you need to do is, either change the variable name in interface or class Ques2, or refer str in class Ques using a "." for eg. Ques1.str.
Hope this helps
Warm Regards
Mandar
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harwinder,
Class Ques inherits two fields with the same name, one from the superclass Ques02 and one from the interface Ques01. The JLS says (8.3.3.3) the fields are ambiguously inherited and therefore may not be referred to by simple name.
Would you say the fields are not ambiguously inherited, and the compiler should determine from context which field is meant?
Would you say two fields with the same name inherited from two different interfaces are ambiguously inherited, but two fields inherited from a superclass and an interface are not ambiguously inherited?
Is referring to an instance field in a static context a common mistake? (I think so.) If it is, then considering fields with the same name to be unambiguous would hide this common error.
[ January 20, 2004: Message edited by: Marlene Miller ]
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic