The following code is an example of achieving multiple class inheritance through the use of interfaces. My question is- What if the two interfaces I and J had a method having same name i.e. show(). How would I now implement multiple class inheritance. I have tried to achieve this using the following code. But it is not working.
interface I
{
void show();
}
interface J
{
void show();
}
class A : I
{
public void show()
{
Console.WriteLine("I");
}
}
class B : J
{
public void show()
{
Console.WriteLine("J");
}
}
class C : I, J
{
A x = new A();
B y = new B();
public void I.show()
{
x.show();
}
public void J.show()
{
y.show();
}
}
Pankaj PandeyPosted May 28, 2013, 12:43 AM
Raj KumarPosted May 27, 2013, 11:44 PM
VulpesPosted May 27, 2013, 6:52 PM
However, there are two things to note about EII:
1. The implemented method cannot have an access modifier.
2. The implemented method can only be accessed via a reference to the interface concerned - you can't access it via a class reference.
Taking these points into account, the following is a working version of the program: