Why interface method doesn't hide System.Object method?

A

Alex Sedow

Example 1

interface I
{
string ToString();
}
public class C : I
{
public void f()
{
ToString(); // IL: callvirt instance string object::ToString()
}
}

Interface I (like any other interface) implicitly derived from
System.Object.
Why interface ToString() method doesn't hide System.Object.ToString()
method?

//--------------------------------------------------------------------------
----

Example 2

interface I
{
void f();
}
public class B
{
public virtual void f()
{}
}
class D : B, I
{
void g()
{
f(); // call B.f();
}
}

Class D inherit to methods with the same signatures: first - from class B,
and second - from interface I.
Why f() call is not ambiguous?

//--------------------------------------------------------------------------
----

And last question. Is members declared in base class has more priority than
members declared in base interfaces?

Alex.
 
A

Alex Sedow

The answer is: Members declared in inherited interfaces are invisible in
classes/structs that impelements these interfaces.

interface I
{
void f();
}
public class B : I
{
void I.f()
{}
}
class D : B
{
void g()
{
f(); // error CS0103: The name 'f' does not exist in the class or
namespace 'D'
}
}

Questions removed.

Alex.
 
M

Morten Wennevik

Hi Alex,

You seem to be somewhat confused of what interfaces actually do.

Example 1

interface I
{
string ToString();
}
public class C : I
{
public void f()
{
ToString(); // IL: callvirt instance string object::ToString()
}
}

Interface I (like any other interface) implicitly derived from
System.Object.
Why interface ToString() method doesn't hide System.Object.ToString()
method?

I myInterface = new B();
myInterface.ToString();

Any class implementing I is contracted to have the method ToString() with a return value of string. You don't actually call the interface's ToString. It is B's ToString() that will be called, and since you haven't written your own ToString() the default Object.ToString() will be used.

An interface will never hide anything, it merely tells "the world" that you can use a set of methods and properties on any object implementing the interface. The properties and methods might be inherited or you might need to write them, but the interface doesn't care and doesn't know.

In any case, since all objects inherit from Object, all objects have ToString(). You can however make your own version of ToString()

public class C : I
{
public void f()
{
ToString();
}

public override ToString()
{
return "Hello World!";
}
}

However, the interface would not tell anything about wether C has its own ToString method or if the base parent Object's ToString will be used instead.

//--------------------------------------------------------------------------
----

Example 2

interface I
{
void f();
}
public class B
{
public virtual void f()
{}
}
class D : B, I
{
void g()
{
f(); // call B.f();
}
}

Class D inherit to methods with the same signatures: first - from class B,
and second - from interface I.
Why f() call is not ambiguous?

Same as above, you never call the methods in the interface directly. The interface will pass along any call to the class.

I myInterface = new D();
myInterface.f();

Same as above where class C inherited from Object. Only with a different method. Where Object had ToString(), B has f();
//--------------------------------------------------------------------------
----

And last question. Is members declared in base class has more priority than
members declared in base interfaces?

Alex.

Not Applicable. Interfaces has no priority because they only describe what can be found in a class that implements that interface.

If, considering the last code sample, you didn't inherit from B. Then you would get a compiler error since there would be no f() available. Since you implement the interface I, you HAVE TO make available all methods and properties described in that interface. You would need to write the method in class D.

interface I
{
void f();
}
class D : I
{
void g()
{
}
}

This would not work. By implementing I you promise to have the method f() described in the interface.
When you inherited from B that was ok, since B had the method f(), but since there isn't any method f() in the base class Object you will need to write method f in class D.
 
A

Alex Sedow

Hello Morten,
You seem to be somewhat confused of what interfaces actually do.

<skip>

Thanks for the answer. Now I understand sense of interfaces little bit
better.

Alex.
 
A

Aaron Robson

Hi Alex,
An interface does not 'implement' a method, it only provides declarations which a class must implement to fullfill the interface. The ambiguities you are worried about do not exist, as the interfaces don't actually supply an implementation.

In Example 1, class C implements the I.ToString() method via its implicit base class System.Object, so there is only one implementation of ToString available.
Note that if class C was to provide an alternative implementation, it would have to be defined as either:
public override string ToString() {...}
or
public new string ToString() {...}

[ I think there is also another option of defining an explicit or private interface implementation as string I.ToString() {...}, but I won't go into this ]

Hope this is of help.

Aaron.



nntp://news.dsl.pipex.com/microsoft.public.dotnet.languages.csharp/ >

Example 1

interface I
{
string ToString();
}
public class C : I
{
public void f()
{
ToString(); // IL: callvirt instance string object::ToString()
}
}

Interface I (like any other interface) implicitly derived from
System.Object.
Why interface ToString() method doesn't hide System.Object.ToString()
method?

//--------------------------------------------------------------------------
----

Example 2

interface I
{
void f();
}
public class B
{
public virtual void f()
{}
}
class D : B, I
{
void g()
{
f(); // call B.f();
}
}

Class D inherit to methods with the same signatures: first - from class B,
and second - from interface I.
Why f() call is not ambiguous?

//--------------------------------------------------------------------------
----

And last question. Is members declared in base class has more priority than
members declared in base interfaces?

Alex.




[microsoft.public.dotnet.languages.csharp > ]
 
F

Frans Bouma [C# MVP]

Alex said:
The answer is: Members declared in inherited interfaces are invisible in
classes/structs that impelements these interfaces.

interface I
{
void f();
}
public class B : I
{
void I.f()
{}
}
class D : B
{
void g()
{
f(); // error CS0103: The name 'f' does not exist in the class or
namespace 'D'
}
}


of course, as you implement the interface method explicitly and private.

If B looked like this:

public class B:I
{
public void f()
{}
}

it would compile.

Frans.
 

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

Top