By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,226 Members | 1,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,226 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Nov 16 '05 #2

P: n/a
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]
Nov 16 '05 #3

P: n/a
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.

Nov 16 '05 #4

P: n/a
You might want to read a previous description of interfaces I made some time back :)

http://groups.google.com/groups?hl=e...ord%26rnum%3D1

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

P: n/a
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 > ]
Nov 16 '05 #6

P: n/a
Alex Sedow wrote:
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.

--
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.