Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Implementing two interfaces having same variable.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I know that multiple inheritance is not allowed in java.
I like to know what will happen for the following scenario:
There are two interfaces InterfaceA and InterfaceB.
Both these interfaces have a variable named 'i',and 'i' takes the value of 10 in InterfaceA and 20 in InterfaceB.
I am implementing these two interfaces in a class say Class1.
If I try to print the value of 'i' in Class1 what value will be printed?

Thanks,
Shubakarthik
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The Java Language Spec is quite clear on describing this, and related cases. In the scenario you describe, any code in Class1 that refers to i must use "InterfaceA.i" or "InterfaceB.i". Just plain unadorned "i" will not be allowed by the compiler because it is ambiguous.
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
The Java Language Spec is quite clear on describing this, and related cases. In the scenario you describe, any code in Class1 that refers to i must use "InterfaceA.i" or "InterfaceB.i". Just plain unadorned "i" will not be allowed by the compiler because it is ambiguous.



In cases of two interfaces, this is correct. In a single interface case, it is allowed but highly discouraged as poor style.

BTW, keep in mind that fields in interfaces must be static and final. Naming conventions hence follow those of constants -- ALL_CAPS_WITH_WORDS_SEPARATED_BY_BLANKS.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, this is one of those questions which is best answered by simply trying it...
reply
    Bookmark Topic Watch Topic
  • New Topic