File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes same methods in two interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "same methods in two interface" Watch "same methods in two interface" New topic
Author

same methods in two interface

Soman Singh
Ranch Hand

Joined: Sep 02, 2003
Posts: 37
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

Joined: Nov 22, 2008
Posts: 18944
Have you tried to compile it ?? What happened ?
Mathews P Srampikal
Ranch Hand

Joined: Nov 26, 2002
Posts: 211
Have checked out with the collection interfaces......and try to implement the 2 interfaces which have 2 methods same signatures and run.


Thanks,
Mathews
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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!


[Jess in Action][AskingGoodQuestions]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: same methods in two interface
 
Similar Threads
MULTIPLE INHERITANCE IN JAVA
how to implement two interfaces having same methods
Multiple interfaces
HOW TO IMPLEMENT TWO SAME METHODS FROM DIFFERENT INTERFACES IN ONE CLASS
One class implement two interfaces with common methods??