-
August 14th, 2008, 04:07 PM
#1
2 interfaces with same method
hi all,
i had a problem if suppose there are two interfaces with same method name say
interface A
{
public void show();
}
interface B
{
public void show();
}
and class C implements A,B
{
public void show()
{
.................Do something........
}
PSVM(String s[])
{
}
}
my question is which interface method is implemented.
thanks in advance.
-
August 14th, 2008, 04:20 PM
#2
Re: 2 interfaces with same method
Have you coded and compiled it to see what the compiler says?
If the two methods have the same signature, the compiler should only allow you to have one of them coded in your program.
Try it and let us know.
Last edited by Norm; August 14th, 2008 at 04:22 PM.
Norm
-
August 14th, 2008, 04:48 PM
#3
Re: 2 interfaces with same method
thanks for yr quick reply my frnd i do have compiled and run the program.i needs to know the implementation is from which interface either A's method is implemented or B's mehod in my Netbeans IDE its show the method which is implemented firts
for eg.
class c implements A,B
then the compilers says imeplemntation of interface A ..but i am unable to get the theory behind it plz help.
thanks in advance.
-
August 14th, 2008, 05:08 PM
#4
Re: 2 interfaces with same method
If you get error messages, please copy and paste them here.
I would think that the show() method you show would satisfy both interfaces, but to be sure, write a program and see what happens. Have you done that? If so what happened?
I have no idea what any IDE would do with it.
Norm
-
August 14th, 2008, 05:16 PM
#5
Re: 2 interfaces with same method
yes dear frnd i have done that,the program is running fine now my question is out of interfaces A or B whose method is getting imeplented in the implamentation class.
thanks in advance.
-
August 14th, 2008, 05:54 PM
#6
Re: 2 interfaces with same method
If the two interfaces have a common method (same signature), then you just implement it and it will be available via references to both interfaces.
The only problem arises if the intended action of the method is significantly different in the two interfaces. You clearly can only provide one implementation, so it's possible that the method will do the 'wrong' thing from the point of view of one interface.
In theory, there is no difference between theory and practice, but not in practice...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
August 14th, 2008, 07:52 PM
#7
Re: 2 interfaces with same method
Originally Posted by javaplayer
hi all,
my question is which interface method is implemented.
Simply both.
This is the so called diamond problem. In Java its solved technically because there can ever be only one implementation for the name clashing methods.
From a design point of view it's still a problem though. Do you really want the methods of two different interfaces to be implemented in the exactly same way? The answer probably is no and then the only solution is to rename the methods so they can be implemented differently.
So the Java inheritance model solves the diamond inheritance problem for the compiler but not for the designer.
Last edited by _uj; August 14th, 2008 at 07:55 PM.
-
August 15th, 2008, 10:49 AM
#8
Re: 2 interfaces with same method
thanks a lot for your all's valuable suggestions i think now its clear to me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|