2 interfaces with same method
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: 2 interfaces with same method

  1. #1
    Join Date
    Aug 2008
    Posts
    4

    Question 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.

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    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

  3. #3
    Join Date
    Aug 2008
    Posts
    4

    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.

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    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

  5. #5
    Join Date
    Aug 2008
    Posts
    4

    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.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: 2 interfaces with same method

    Quote 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.

  8. #8
    Join Date
    Aug 2008
    Posts
    4

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center