Interface & Derived Return Type

H

hufaunder

I have a class "TestSuper" that implements the interface "TestBase".
The interface has a property of type "ReturnType". The class
"TestSuper" does not return "ReturnType" but a derivation
"ReturnSuper". This gives the following compile error due to a bad
return type:

TestSuper does not implement interface member TestBase.Func.
TestSuper.Func is either static, not public, or has the wrong return
type.

Why is it not possible that the derivated type "ReturnSuper" can be
defined as the return type in the class "TestSuper"?


class ReturnBase{}
class ReturnSuper : ReturnBase { }

interface TestBase
{
ReturnBase Func { get; }
}

class TestSuper : TestBase
{
public ReturnSuper Func { get { return new ReturnSuper(); } }
}
 
M

Michael C

I have a class "TestSuper" that implements the interface "TestBase".
The interface has a property of type "ReturnType". The class
"TestSuper" does not return "ReturnType" but a derivation
"ReturnSuper". This gives the following compile error due to a bad
return type:

TestSuper does not implement interface member TestBase.Func.
TestSuper.Func is either static, not public, or has the wrong return
type.

Why is it not possible that the derivated type "ReturnSuper" can be
defined as the return type in the class "TestSuper"?

That's just the way it is. *You* defined an interface to return TestBase,
you have to stick to that. If you inherit instead of implement you can
redefine the function however (using the new keyword).

Michael
 
A

AlanT

I have a class "TestSuper" that implements the interface "TestBase".
The interface has a property of type "ReturnType". The class
"TestSuper" does not return "ReturnType" but a derivation
"ReturnSuper". This gives the following compile error due to a bad
return type:

Is it required that the return type of Func() be a ReturnSuper or will
it suffice that it returns a ReturnSuper while the return type is
ReturnBase ?

i.e.

class ReturnBase { }
class ReturnSuper : ReturnBase { }

interface TestBase {
ReturnBase Func { get; }
}

class TestSuper : TestBase {
public ReturnBase Func { get { return new ReturnSuper(); } }
-----------------
}

It still returns a ReturnSuper but conforms to the interface.

This does mean that if you have


TestClass test = new TestClass();

ReturnBase aBase = test.Func; // works fine
ReturnSuper aSuper = test.Func; // gives an error


The interface says that the return type is an ReturnBase. If you want
to be able to assign to a ReturnSuper then you should revisit the
interface definition.

hth,
Alan.
 
T

Terry Rogers

I have a class "TestSuper" that implements the interface "TestBase".
The interface has a property of type "ReturnType". The class
"TestSuper" does not return "ReturnType" but a derivation
"ReturnSuper". This gives the following compile error due to a bad
return type:

TestSuper does not implement interface member TestBase.Func.
TestSuper.Func is either static, not public, or has the wrong return
type.

Why is it not possible that the derivated type "ReturnSuper" can be
defined as the return type in the class "TestSuper"?

class ReturnBase{}
class ReturnSuper : ReturnBase { }

interface TestBase
{
ReturnBase Func { get; }
}

class TestSuper : TestBase
{
public ReturnSuper Func { get { return new ReturnSuper(); } }
}

You can do this using an explicit interface implenetation:

class TestSuper : TestBase
{
// public property
public ReturnSuper Func { get { return new ReturnSuper(); } }
// explicit interface implenetation
ReturnBase TestBase.Func { get { return this.Func; } }
}

Then you get a different return type depending on whether the class or
the interface is used:
TestSuper test = new TestSuper();
ReturnSuper value = test.Func; // ok, calling TestSuper.Func

ITest test = new TestSuper();
ReturnSuper value = test.Func; // compiler error, as calling
ITest.Func which returns ReturnBase not ReturnSuper


Terry
 
H

hufaunder

You can do this using an explicit interface implenetation:

class TestSuper : TestBase
{
// public property
public ReturnSuper Func { get { return new ReturnSuper(); } }
// explicit interface implenetation
ReturnBase TestBase.Func { get { return this.Func; } }

}

Then you get a different return type depending on whether the class or
the interface is used:
TestSuper test = new TestSuper();
ReturnSuper value = test.Func; // ok, calling TestSuper.Func

ITest test = new TestSuper();
ReturnSuper value = test.Func; // compiler error, as calling
ITest.Func which returns ReturnBase not ReturnSuper

Terry- Hide quoted text -

- Show quoted text -

I realise that the return type of the function has to match the
interface. What I do not know is why this is so. Two functions cannot
just differ in the type of the return argument. Now, I am not
suggesting that I can just use any return type. But ReturnSuper is a
ReturnBase with some additions. So why can I not use a more
specialized type?

Now why would I want to do that? The reason is that each
implementation of the interface "TestBase" might return a different
specialized type of "ReturnBase". If I use one of these classes based
on "TestBase" directly (TestSuper mySuper) rather then through the
interface (TestBase mySuper=new TestSuper) the compiler could do more
checking during compile time. Or do I miss something?

Thanks
 
P

Pavel Minaev

On Apr 18, 2:23 am, Terry Rogers <roge
I realise that the return type of the function has to match the
interface. What I do not know is why this is so. Two functions cannot
just differ in the type of the return argument. Now, I am not
suggesting that I can just use any return type. But ReturnSuper is a
ReturnBase with some additions. So why can I not use a more
specialized type?

What you want is called "return type covariance", and the lack of support
of it is a known deficiency in the C# language: see
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=90909,
and notice the status is "postponed".

Using explicit interface implementation, as suggested by Terry, deals with
the problem more or less adequately and in a typesafe manner - have
another look at his example. However, this only works when implementing
interfaces, not when overriding methods of base classes. There's no proper
solution to the latter problem, to the best of my knowledge, not even in
C# 3.0.
 
H

hufaunder

What you want is called "return type covariance", and the lack of support
of it is a known deficiency in the C# language: see http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?...,
and notice the status is "postponed".

Using explicit interface implementation, as suggested by Terry, deals with
the problem more or less adequately and in a typesafe manner - have
another look at his example. However, this only works when implementing
interfaces, not when overriding methods of base classes. There's no proper
solution to the latter problem, to the best of my knowledge, not even in
C# 3.0.

It seems I'm not the only one who would like that then. I guess I'll
just use Terry's approach then.

Thanks
 
M

Michael C

Pavel Minaev said:
There's no proper solution to the latter problem, to the best of my
knowledge, not even in C# 3.0.

The new keyword is a partial solution at least.
 
J

Jon Skeet [C# MVP]

Michael C said:
The new keyword is a partial solution at least.

Could you explain how that helps in the case of overriding a base class
method? If you try to change the return type with a "new" method,
you'll get:

<quote>
Test.cs(13,23): warning CS0109: The member 'Derived.Foo()' does not
hide an inherited member. The new keyword is not required.
</quote>

Can you give some sample code?
 
M

Michael C

Jon Skeet said:
Could you explain how that helps in the case of overriding a base class
method? If you try to change the return type with a "new" method,
you'll get:

<quote>
Test.cs(13,23): warning CS0109: The member 'Derived.Foo()' does not
hide an inherited member. The new keyword is not required.
</quote>

Can you give some sample code?

No, I'm confusing things. Now I think about it properly this only worked
with parameters, not the return type.

Michael
 

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