aspose file tools*
The moose likes Beginning Java and the fly likes If one class implements 2 interfaces having same method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "If one class implements 2 interfaces having same method..." Watch "If one class implements 2 interfaces having same method..." New topic
Author

If one class implements 2 interfaces having same method...

Div Raj
Greenhorn

Joined: Nov 04, 2008
Posts: 22
Hi,

Suppose I've 2 interfaces having same method, say A();.
If one class implements both the interfaces, then what will happen when its object calls the method A()?

could anyone clear my doubt?

Thanks
--------
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Your implementing class will have one method A and this will satisfy the requirements of both interfaces. It doesn't matter what type of reference variable you use to call it, the same method will always be executed.

Of course you could have found all this out by writing a little test program.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
The two methods with the same signature must have the same intent in their respective interfaces, and must have the same return type.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11495
    
  16

think of it this way... if you promise your friend Thomas that you will wear a red shirt tomorrow, then you later promise your friend Michael you will wear a red shirt tomorrow, do you have to wear two red shirts?

no.

It's the same with interfaces. you're promising to implement a method, you're just promising twice.
The two methods with the same signature must have the same intent in their respective interfaces,

Campbell, we're gonna have another disagreement. I think "must" is too strong a word. "Should" or "Hopefully will" i think would be better. nothing forces them to be similar - it is not a requirement.

Now, if they don't, you're gonna have problems somewhere, but 'must' is just not a word i would use to describe this.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

I would say must. Not as far as syntax is concerned but if the intent of one of the methods is not adhered to, its contract is broken and the code can be considered as broken.

Using your analogy, if I promised Michael to wear a blue shirt instead of a red shirt, and I can't wear two shirts, then I will have to break at least one promise. The same can hold for the methods: if keeping one contract would mean breaking the other then it's in fact a bad idea to implement both interfaces.

Instead you can choose to create a method that returns an object that implements the second interface instead:

[ November 28, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Disagree. "Must" is too strong a word and implies that the program will not work if violated. "Should" better captures the fact that everything will work, it just may not make semantic sense and is poor practice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19781
    
  20

Originally posted by Bear Bibeault:
Disagree. "Must" is too strong a word and implies that the program will not work if violated.

But if the user expects the behaviour of the second interface but gets the behaviour of the first interface, does that not possibly lead to a broken program?

But before we start a lengthy discussion that probably can't be won by either side, let's just say that this case should be prevented at all times. Breaking contracts is bad practice period.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Rob and I seem to be in agreement. Fortunately I think such a collision of incompatible intent will be rare.

I think the problem can be resolved if the interfaces' "intent" is non-specific. Look at the add() method in the List, Collection and Set interfaces, and how vaguely it is defined in Collection. That is an "intent" which is meant to be interpreted freely.

As Rob points out "hopefully will" means you might not fulfil one of the contracts of the interfaces.
 
wood burning stoves
 
subject: If one class implements 2 interfaces having same method...