• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
--------
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two methods with the same signature must have the same intent in their respective interfaces, and must have the same return type.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic