473,796 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface & Derived Return Type

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
"ReturnSupe r". 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 "ReturnSupe r" 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(); } }
}

Apr 18 '07 #1
9 4787
<hu*******@yaho o.comwrote in message
news:11******** **************@ y5g2000hsa.goog legroups.com...
>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
"ReturnSupe r". 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 "ReturnSupe r" 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
Apr 18 '07 #2
On Apr 18, 8:49 am, "hufaun...@yaho o.com" <hufaun...@yaho o.comwrote:
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
"ReturnSupe r". 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.
Apr 18 '07 #3
On Apr 18, 8:49 am, "hufaun...@yaho o.com" <hufaun...@yaho o.comwrote:
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
"ReturnSupe r". 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 "ReturnSupe r" 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

Apr 18 '07 #4
On Apr 18, 2:23 am, Terry Rogers <rogers.te...@g mail.comwrote:
On Apr 18, 8:49 am, "hufaun...@yaho o.com" <hufaun...@yaho o.comwrote:


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
"ReturnSupe r". 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 "ReturnSupe r" 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- 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

Apr 18 '07 #5
On Wed, 18 Apr 2007 20:53:41 +0400, hu*******@yahoo .com
<hu*******@yaho o.comwrote:
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/VisualS...edbackID=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.
Apr 19 '07 #6
On Apr 19, 4:41 am, "Pavel Minaev" <int...@gmail.c omwrote:
On Wed, 18 Apr 2007 20:53:41 +0400, hufaun...@yahoo .com

<hufaun...@yaho o.comwrote:
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/VisualS...Feedback.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

Apr 19 '07 #7
"Pavel Minaev" <in****@gmail.c omwrote in message
news:op******** *******@pminaev .acronisru...
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.
Apr 22 '07 #8
Michael C <no****@nospam. comwrote:
"Pavel Minaev" <in****@gmail.c omwrote in message
news:op******** *******@pminaev .acronisru...
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.
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?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '07 #9
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
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
Apr 23 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8266
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it at all possible?
7
3680
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b; but it complains for the following lines
20
4263
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
10
2982
by: Brett | last post by:
I'm still trying to figure out concrete reasons to use one over the other. I understand the abstract class can have implementation in its methods and derived classes can only inherit one abstract class. The interface has implied abstract methods/properties and derived classes can inherit multiple interfaces. The interface properties/methods have no implementation. Besides definitions of the two, what are some conceptual reasons to use...
18
3785
by: Bradley | last post by:
I'm trying to determine if there's a general rule for when an Interface should used vs. an Abstract Class. Is there any design advantage to using one or the other? Brad
4
2861
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
3
4552
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
6
2041
by: AzizMandar | last post by:
There is probably a better way to do this and if so I'm just as happy to see that way. I have a program where I have factories that each create various objects abstracted from a base class. The Factories all have a base class as well for the objects to point back to. So I have
3
2128
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10190
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.