GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 366 of 436    Print  
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.

  
Total Answers and Comments: 7 Last Update: March 02, 2007     Asked by: Nitesh 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 07, 2006 13:51:34   #1  
balaji        

RE: If we have 2 interfaces, say Ix and Iy, and also w...

Hai

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


 
Is this answer useful? Yes | No
October 18, 2006 05:43:55   #2  
Amit Kumar dwivedi        

RE: If we have 2 interfaces, say Ix and Iy, and also w...

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


 
Is this answer useful? Yes | No
November 19, 2006 00:36:24   #3  
PavanKumar        

RE: If we have 2 interfaces, say Ix and Iy, and also w...
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.
 
Is this answer useful? Yes | No
November 23, 2006 03:56:15   #4  
BinduJoseph Member Since: June 2006   Contribution: 4    

RE: If we have 2 interfaces, say Ix and Iy, and also w...
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

 
Is this answer useful? Yes | No
November 25, 2006 06:11:31   #5  
John        

RE: If we have 2 interfaces, say Ix and Iy, and also w...

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


 
Is this answer useful? Yes | No
December 22, 2006 22:19:58   #6  
swatias Member Since: December 2006   Contribution: 5    

RE: If we have 2 interfaces, say Ix and Iy, and also w...

We can inherit the methods of lx & ly interfaces using lx.method() and ly.method().

Reference: http://msdn2.microsoft.com/en-us/library/ms228387(VS.80).aspx


 
Is this answer useful? Yes | No
March 02, 2007 01:26:07   #7  
DilpreetSachdev Member Since: March 2007   Contribution: 1    

RE: If we have 2 interfaces, say Ix and Iy, and also w...
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.





 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape