| Author |
Implementing two interfaces having same variable.
|
shuba karthik
Greenhorn
Joined: Sep 03, 2004
Posts: 14
|
|
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
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
David Hibbs
Ranch Hand
Joined: Dec 19, 2002
Posts: 374
|
|
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.
|
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
BTW, this is one of those questions which is best answered by simply trying it...
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: Implementing two interfaces having same variable.
|
|
|