File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

same methods in two interface

 
Soman Singh
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a simple question.
I have declared two methods in two different interfaces. The return of
is same for both the methods i.e they are same.
I implement both the interfaces in a class.
And I also use the methods in the class.
Now
Will it compile .... ->
If it does then how will the class know which interface has to be looked for the method.
Any clues shall be put at the top of Appreciation ....
Bye !!!
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Have you tried to compile it ?? What happened ?
 
Mathews P Srampikal
Ranch Hand
Posts: 211
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have checked out with the collection interfaces......and try to implement the 2 interfaces which have 2 methods same signatures and run.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Soman
You would be required to spell out the exact path. That is, if you have callStack() method in both ProdArray interface and AssyArray interface then you would be required to call it like ProdArray.callStack() and AssyArray.callStack().
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anumpam --
No, totally wrong. Java isn't C++.
Soman --
This case is explicitly addressed in the JLS. The answer is that if the methods can both be satisfied by a single implementation (i.e., their exception signatures are identical too, or close enough) then this will compile and work; but if the return types or exception signatures are incompatible, then it simply can't be done.
The question of "how does it know which to call" can't and won't come up. Java only has single inheritance, so there can only ever be at most a single inherited version of a method, which you can override, or not. There's no way for code outside of a class to call an inherited method that's been overridden. So whichever interface you think you're using, there will only be a single method, and it better make sense for both interfaces!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupam Sinha:
You would be required to spell out the exact path. That is, if you have callStack() method in both ProdArray interface and AssyArray interface then you would be required to call it like ProdArray.callStack() and AssyArray.callStack().

Actually no. If you implement two interfaces with the same method then no one cares that you are only implementing one method. The interface contract has been satisfied for both. The only problem is if the methods have different return types. In that case you will not be able to compile your code.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic