Hi Alex,
You seem to be somewhat confused of what interfaces actually do.
On Wed, 25 Aug 2004 12:57:04 +0400, Alex Sedow <al*******@mail.ru> wrote:
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.
--
Happy Coding!
Morten Wennevik [C# MVP]