If we have 2 interfaces, say Ix and Iy, and also we have one method in both interfaces with same signature. Now we have to inherit a class from both interfaces so how to access the method of interface Ix or that of Iy.

Showing Answers 1 - 17 of 17 Answers

balaji

  • Oct 7th, 2006
 

Hai,

  Class can inherit those two  interfaces IX,IY which has methods with same signature by specifying  IX.methodname() and IY.methodname()

  Was this answer useful?  Yes

Amit Kumar dwivedi

  • Oct 18th, 2006
 

  Example

   interface lx

   {

         void Amit();

   }

   interface ly

   {

      void Amit();

   } 

 class Dwivedi:lx,ly

   {

          public static void main()

           {

                    lx k;

                    ly l;

                     k.Amit();

                     l.Amit();

           }

}

That's the right example we can use for implementing both interfaces

 

  Was this answer useful?  Yes

PavanKumar

  • Nov 19th, 2006
 

If we have 2 interfaces, say Ix and Iy, and also we have one method in both interfaces with same signature. Now we have to inherit a class from both interfaces so how to access the method of interface Ix or that of Iy. This is one of the reasons for the lack of multiple inheritance in C#.Net. In our class if at all we have two interfaces with the Common method name along with the common signature than only one of the method is accessible for the class that is inheriting the interfaces.interface ix{public void Paint();}interface iy{public void Paint();}Class MyClass : ix , iy // If this is the sequence in which we are inheriting { //Then only the method of firstly inherited interface //(i.e) interface ix's Paint() method is availble //If at all we inherit iy initially inherit iy so that the Paint() method of iy is availaible. }So the inheriting order decides which method to be called.

  Was this answer useful?  Yes

BinduJoseph

  • Nov 23rd, 2006
 

If a class implements two interfaces that each have a method with the same name, say Test(), then

If both Tests have different signatures, then the class implements two overloaded Test methods
If both Tests have the same signature and the same return type, then the class implements only the one Test
If both Tests have the same signature and different return types, then the class can not implement both interfaces
If both Tests have the same signature, same return type but differ in the types of exceptions they throw, then the class implements only the one Test, but it must contain the exceptions both Test's

  Was this answer useful?  Yes

John

  • Nov 25th, 2006
 

HI BinduJoseph,

Can you explain more on what you mean by the following?

If both Tests have the same signature and the same return type, then the class implements only the one Test
If both Tests have the same signature and different return types, then the class can not implement both interfaces

What do you mean by when both Test have the same signature/return type, the class implement only the one Test, what is "the one Test"?

So if both Tests hav the same signature and different return types, then the class will only implement the the method w/ the correct return type, is that correct?

John

  Was this answer useful?  Yes

I think, while answering this question people are missing on the simple OO concepts.

When we inherit from an Interface  say X and it has a method say do() , what you are saying is that any class which implement interface X needs to have method do() so that anybody holding an object by reference if Interface X can call method do().

Interface X
{
    void do();
}


So if you have one more interface Y with method do() again.

Interface Y
{
    void do();
}


So now if any class Implement X & Y interface need to have method do() so that is you are holding the object of this class with reference of X or Y interface , you should be able to call method do() on it.

Class MyClass : X,Y
{
    void do()
    {
        ...
    }
}


MyClass myObject = new MyClass();
myObject.do();

X xObject = myObject;
xObject.do();

Y yObject= myObject;
yObject.do();

So the point is while implementing Interfacing I need to satisfy the contract that the class will implement methods in the interfaces , and if its same method signature then its always one implementaion of it in the class .

when I said Method signature is same I mean return type , method name , and all parameters type and sequence is same.




  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions