locked
Interface doubt Same method names -- Explicit interfaces RRS feed

  • Question

  • Hi Experts,

      I have the following code.   I am sorry for asking this basic question. 

    namespace ExampleInterfaces
    {
        interface Square
        {
            int Area(int side);
        }
        interface Rectangle
        {
            int Area(int Width, int Height);
        }
        interface Circle
        {
            int Area(int radius);
        }
        class Program1 : Square, Rectangle, Circle
        {      
            public int Area(int radius)
            {
                return ((radius * radius) * (22 / 7)); //Some logic
            }
            public int Area(int width, int height)
            {
                return ((width * height));
            }
            static void Main(string[] args)
            {
                Program1 pobj = new Program1();
                Console.WriteLine(pobj.Area(10));
            }
      }
    }
     

    My doubt is I am inheriting Program1  class  from three interfaces such as Circle, Squaer and Rectangle.  Square and Circle interfaces having same function name and parameters. I am creating object of Program1 class and calling Area() method.

    Note that I am not calling like this following way.

    Square s = new Program1() ; OR Circle cs = new Program1();

    I was expecting compile time error when run, but I run successfully. My dubt is I have not mentioned which interface method is implemented in program1 class. So which interface method is executed in the following code:

     public int Area(int radius)
            {
                return ((radius * radius) * (22 / 7)); //Some logic
            }

    Thanks in Advance

    Regards,

    T.N.Nagasundar

    Thursday, November 22, 2012 2:05 PM

Answers

  • "then Circle interface area method will also be invoked?"

    Yes, you could say that. But strictly speaking there's no such thing as the Circle's Area method, there's only one Area(int) method: Program.Area(int). It's not like if you implement an interface method your implementation somehow gets copied into the interface method and results in duplicated methods in cases like your. The C# specification basically says that both Circle.Area and Square.Are are mapped to Program.Area.

    @Shishir DK: "The interfaces having same Method name and same parameter list will look like overloaded methods in the class"

    This has nothing to do with overloading. Method overloading implies same method name but different signature.

    "So even though syntactically this looks good. This is not so correct according to Object oriented programing."

    There's nothing wrong for 2 interfaces to contain members with the same name. It can happen and it will happen.

    "It is always better to have Identifiable names for the method in the interface and then try implementing as shown below."

    OOP wise the whole example is dubious to begin with. Renaming the interface methods won't fix it because that's not the problem, in fact it may make it worse. Anyway, I don't think this is relevant to the actual question.

    • Marked as answer by Nagasundar TN Monday, November 26, 2012 2:58 PM
    Friday, November 23, 2012 8:54 AM

All replies

  • "Note that I am not calling like this following way..."

    So basically you aren't using any of those 3 interfaces for calling, you directly call the Area(int) method of the Program class. You could delete all the interfaces from your code and the program will still compile and run the same as before.

    "Square and Circle interfaces having same function name and parameters."

    In this case the Area(int) method of Program implements both Square.Area and Circle.Area. It's perfectly valid for a single method to implement methods from multiple interfaces.

    • Proposed as answer by Pieter Geerkens Thursday, November 22, 2012 10:32 PM
    Thursday, November 22, 2012 2:44 PM
  • Because pobj is defined as being of tyoe Program1, none of the interface methods is invoked!

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Thursday, November 22, 2012 2:46 PM
  • Dear Mike,

       Thanks for your reply. I was expecting the following Compile time error:

    'ExampleInterfaces.Program1' does not implement interface member 'ExampleInterfaces.Square.Area(int)'

    OR

    'ExampleInterfaces.Program1' does not implement interface member 'ExampleInterfaces.Circle.Area(int)'

       Yes.   I am not calling through interface reference. But I didn't implement methods of all interfaces from where my Program1 class inherits.

       You mentioned " It's perfectly valid for a single method to implement methods from multiple interfaces. " As per my understanding you mean to say, the Area(int) method is implemented from interfaces Square and Circle. Suppose if I call like this

     Square s = new Program1() ;

     s.Area(10);

     then Circle interface area method will also be invoked? ( I am sorry I am not so clear with that). Could you please explain me or send me any referecen links ?

    Regards,

    T.N.Nagasundar

       


    Friday, November 23, 2012 5:26 AM
  • The key concept is that the name of the formal parameter in an interface method declaration is ignored for purposes of determining its signature; it is only a comment. Thus these two interfaces

        interface Square
        {
            int Area(int side);    // ignore side
        }
        interface Circle
        {
            int Area(int radius);  // ignore radius
        }
    have the identical signature, so are the same set of methods by two distinct names.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer


    • Edited by Pieter Geerkens Friday, November 23, 2012 5:34 AM
    • Proposed as answer by Shishir DK Friday, November 23, 2012 6:35 AM
    Friday, November 23, 2012 5:33 AM
  • Hi Nagasundar,

    If I understand  your doubt, you mean to say how even though not all interface is implemented in Program1 only one method implementation public int Area(int radius)
     is taken for both interface interface Square as well as interface Circle which has difference area calculation formula.

    The interfaces having same Method name and same parameter list will look like overloaded methods in the class .So even though syntactically this looks good. This is not so correct according to Object oriented programing.

    As Mr Pieter has rightly pointed out the same point.It is always better to have Identifiable names for the method in the interface and then try implementing as shown below.
    Hope this has helped you clear your doubts.

    namespace ExampleInterfaces
    {
        interface Square
        {
            int SquareArea(int side);
        }
        interface Rectangle
        {
            int RectangleArea(int Width, int Height);
        }
        interface Circle
        {
            int CircleArea(int radius);
        }
    
    
        class Program1 : Square, Rectangle, Circle
        {
            static void Main(string[] args)
            {
                Program1 pobj = new Program1();
                Console.WriteLine(pobj.SquareArea(10));
                Console.Read();
            }
    
            public int SquareArea(int side)
            {
                return ((side * side));
            }
    
            public int RectangleArea(int Width, int Height)
            {
                return ((Width * Height));
            }
    
            public int CircleArea(int radius)
            {
                return ((radius * radius) * (22 / 7)); //Some logic
            }
        }
    }

    • Proposed as answer by Shishir DK Friday, November 23, 2012 6:36 AM
    Friday, November 23, 2012 6:26 AM
  • "then Circle interface area method will also be invoked?"

    Yes, you could say that. But strictly speaking there's no such thing as the Circle's Area method, there's only one Area(int) method: Program.Area(int). It's not like if you implement an interface method your implementation somehow gets copied into the interface method and results in duplicated methods in cases like your. The C# specification basically says that both Circle.Area and Square.Are are mapped to Program.Area.

    @Shishir DK: "The interfaces having same Method name and same parameter list will look like overloaded methods in the class"

    This has nothing to do with overloading. Method overloading implies same method name but different signature.

    "So even though syntactically this looks good. This is not so correct according to Object oriented programing."

    There's nothing wrong for 2 interfaces to contain members with the same name. It can happen and it will happen.

    "It is always better to have Identifiable names for the method in the interface and then try implementing as shown below."

    OOP wise the whole example is dubious to begin with. Renaming the interface methods won't fix it because that's not the problem, in fact it may make it worse. Anyway, I don't think this is relevant to the actual question.

    • Marked as answer by Nagasundar TN Monday, November 26, 2012 2:58 PM
    Friday, November 23, 2012 8:54 AM
  • Dear Mike,

     Thanks for your reply

    Monday, November 26, 2012 2:59 PM
  • Dear Pieter,

     Thanks for your reply

    Monday, November 26, 2012 3:00 PM
  • Dear Shishir,

       Thanks for your reply. My doubt was if we have same method name with sameparamaters  in two different interfaces and if both interafaces implmented then which will be called. Yes to avoid confusion we can use naming conventions. Thanks once again..

    Monday, November 26, 2012 3:03 PM