Multiple Interface!!!!!!!

  • Thread starter Thread starter Wallace
  • Start date Start date
W

Wallace

Hai All,

I am having some query in the following sample code... On creating
object for class1 and calling display function, it shows a message box
with "Hai". My question is

1. Which interface's display is called?
2. If I want to give different implementation for each display(), how
can I do that?
3. Again ambiguity occurs, how can differenciate from multiple
inheritance?

interface ITest1
{
void display();
}

interface ITest2
{
void display();
}

class class1 : ITest1, ITest2
{
public void display()
{
MessageBox.Show("Hai");
}
}

someone plz explain the concept here....
Looking forward for the response...
Thanx in advance...
 
As it stands, your "Hai" display method() will be called regardless of
whether somebody is looking at your object as a class1, an ITest1 or an
ITest2

If you want to provide separate implementations, or you just don't want to
expose a particular interface method on the class's public interface, then
you use explicit interface implementation, like so:

class class1 : ITest1, ITest2 {
public void display() {
MessageBox.Show("Fred");
}
void ITest1.display() {
MessageBox.Show("Barney");
}
void ITest2.display() {
MessageBox.Show("Wilma");
}
}

Here, I will get 3 different messages:
class1 obj = new class1();
obj.display();
((ITest1) obj).display();
((ITest2) obj).display();

Marc
 
Wallace said:
Hai All,

I am having some query in the following sample code... On creating
object for class1 and calling display function, it shows a message box
with "Hai". My question is

1. Which interface's display is called?
2. If I want to give different implementation for each display(), how
can I do that?
3. Again ambiguity occurs, how can differenciate from multiple
inheritance?

interface ITest1
{
void display();
}

interface ITest2
{
void display();
}

class class1 : ITest1, ITest2
{
public void display()
{
MessageBox.Show("Hai");
}
}

someone plz explain the concept here....
Looking forward for the response...
Thanx in advance...

It doesn't matter from a pure implementation standpoing. You can, if you
want do explicit interface implementation and provide two display() methods.
But the contract (the interface) simply states you need to provide for a
display() method, so having only one can satisfy both interfaces that you
are implementing. If requirements are such that you do want a separate
implementation for each interface, that is accomplished as follows:

public void ITest1.display(){...}
public void ITest2.display(){...}
 
Hi Wallace,
If you run into a situation like this, you can differentiate the
implementation like this:

class class1 : ITest1, ITest2
{
void ITest1.display()
{
MessageBox.Show("ITest1");
}
void ITest2.display()
{
MessageBox.Show("ITest2");
}
}

And you can differentiate the call like this:

class1 x = new class1();
ITest1 a = (ITest1)x;
ITest2 b = (ITest2)x;
a.display();
b.display();

Hope that helps,
John
 
Hai All,

Thanx for ur explanations!!!!!!!!!

Tom said:
It doesn't matter from a pure implementation standpoing. You can, if you
want do explicit interface implementation and provide two display() methods.
But the contract (the interface) simply states you need to provide for a
display() method, so having only one can satisfy both interfaces that you
are implementing. If requirements are such that you do want a separate
implementation for each interface, that is accomplished as follows:

public void ITest1.display(){...}
public void ITest2.display(){...}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top