This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Implementing two interfaces having same variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Implementing two interfaces having same variable." Watch "Implementing two interfaces having same variable." New topic
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
    
  13

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Implementing two interfaces having same variable.
 
Similar Threads
q on interfaces from overalltesting.com
Why public?
UML: associations of interfaces
Abstract Class vs. Interface
Cyclic class