• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

dout in mock test question

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface Ques01 {
String str = " Ques01 " ;
}

class Ques02 {
String str = " Ques03 " ;
};

class Ques extends Ques02 implements Ques01 {
String a = "popo";
public static void main ( String args [ ] ) {
System . out . println (str ) ;
}
}



It gives a compile time error about the str variable being ambigious .... but according to me the str variable in the interface is implicitly static only that should be considered by main (as only that can be l;egally used)

Please can some one throw some light on it

Thanks
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens is that it inherits both the values from the interface and the superclass...and since they have the same name the compiler cannot decide which one you are referring to...added to that you are referring the variable in the inheritence way...if you refer to the static interface variable the static way then you wont see the error...
put this code and you wont see any errors
System.out.println (Ques01.str ); // this is how you access the static variables (also you can do by creating instances)


Suresh Koutam
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please refer

/**
* It is possible for an interface to inherit more than one field with
* the same name (�8.3.3.3). Such a situation does not in itself cause a
* compile-time error. However, any attempt within the body of the
* interface to refer to either field by its simple name will result in a
* compile-time error, because such a reference is ambiguous.
*/

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gowtham Ganamukala wrote:

... ambiguous ...


Bu.
 
I don't always make ads but when I do they're tiny
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic