473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inheritance and overriding

Hello all,

Why this piece of code doesn't compile?

public class FStorage
{}

public class FSExcel : FStorage
{}

public abstract class IResultLine
{
public abstract void ToExcelLine(FStorage storage);
}
public class Foo
{
internal class ResultLine : IResultLine
{
public sealed override void ToExcelLine(FSExcel storage) {}
}
}
I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

Thanks in advance

Mike
Jun 27 '08 #1
9 1303
Michael wrote:
public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}
My guess is FSExcel and FStorage aren't the same.

Chris.
Jun 27 '08 #2
On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?
To override a method, you have to specify *exactly* the same signature
(in terms of types and parameter modifiers; the parameter names can be
different).

Jon
Jun 27 '08 #3
Michael wrote:
public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}

I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(A
ddIn.View.FileStorage.FSExcel)': no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'
Because the signature of both methods is not the same. What you are
trying to do is called "covariance", and C# does not support that for
method overriding. It is only supported for arrays and delegates (since
C# 2.0).

--
Rudy Velthuis http://rvelthuis.de

"Far too many development shops are run by fools who succeed
despite their many failings." -- Brion L. Webster
Jun 27 '08 #4
On Apr 16, 3:20*pm, Michael <michael.de...@gmail.comwrote:
Hello all,

Why this piece of code doesn't compile?

public class FStorage
{}

public class FSExcel : FStorage
{}

public abstract class IResultLine
{
* * public abstract void ToExcelLine(FStorage storage);

}

public class Foo
{
internal class ResultLine : IResultLine
{
* * public sealed override void ToExcelLine(FSExcel storage) {}

}
}

I have the next errors:

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn..*View.FileStorage.FSExce l)':
no suitable method found to override

'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'

Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

Thanks in advance

Mike
Hi,

public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}

are not the same, they need to have the same signature.
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
Jun 27 '08 #5
On Apr 16, 1:30 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:

<snip stuff about the signature>
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
That makes perfect sense to me. It's often entirely appropriate to say
"I don't want anyone overriding this method any further" but still
having it abstract in the base class.

It's similar to implementing an interface but marking the implementing
method as sealed, which is common in the Dispose pattern.

Personally I'd prefer to seal the whole derived class if possible, but
I have relatively extreme views on inheritance :)

Jon
Jun 27 '08 #6
Ignacio Machin ( .NET/ C# MVP ) wrote:

public abstract void ToExcelLine(FStorage storage);
public sealed override void ToExcelLine(FSExcel storage) {}
Frankly what sense it has to declare a method virtual (which you do by
declaring it abstract) and later declared it as sealed ?
It can make sense, on two grounds: correctness and performance.

If the descendant which overrides and seals the method needs to rely on
the implementation not changing for the rest of the implementation of
that descendant, it may seal the method. Further descendants won't be
able to override that particular method.

For the ancestor declaring the method, and intermediate ancestors,
virtual dispatch for the method will still work as expected.

In terms of performance: if a value that is statically known to be of a
type which declares a method to be sealed, or a descendant of a type
which declares a method to be sealed, virtual dispatch doesn't need to
be performed. A static call may be made instead.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #7
On 16 avr, 21:46, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_.ToExcelLine(AddIn.View.FileStorage.FSExcel) ':
no suitable method found to override
'AddIn.ListOfControls.DerniersRapportsVisite.Resul tLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.Fil eStorage.FStorage)'
Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

To override a method, you have to specify *exactly* the same signature
(in terms of types and parameter modifiers; the parameter names can be
different).

Jon
OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
Jun 27 '08 #8
On Apr 16, 11:23 pm, Michael <michael.de...@gmail.comwrote:

<snip>
OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
In this case it wouldn't be valid even if covariance/contravariance
*did* apply. An override should apply to *all* calls to the method -
so with covariance/contravariance you would be able to override with
*less* specific parameters and a *more* specific return type. You're
trying to override with a *more* specific parameter.

What would you expect to happen if someone called ToExcelLine with an
FStorage which *wasn't* an FSExcel?

Jon
Jun 27 '08 #9
Michael wrote:
On 16 avr, 21:46, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
>On Apr 16, 12:20 pm, Michael <michael.de...@gmail.comwrote:

<snip>
>>'AddIn.ListOfControls.DerniersRapportsVisite.Res ultLine_.ToExcelLine(AddIn.View.FileStorage.FSExce l)':
no suitable method found to override
>>'AddIn.ListOfControls.DerniersRapportsVisite.Res ultLine_' does not
implement inherited abstract member
'AddIn.View.IResultLine.ToExcelLine(AddIn.View.F ileStorage.FStorage)'
>>Why do I have to pass a FStorage object, whereas ResultLine inherits
from IResultLine?

To override a method, you have to specify *exactly* the same
signature (in terms of types and parameter modifiers; the parameter
names can be different).

Jon

OK, thanks for your reply, I didn't know that covariance couldn't be
applied here. Anyway I think that's a strange limitation, because I
don't see what would be a problem for the compiler to deduce that
FSExcel inherits from FStorage...

So I'll have to keep a cast to FSExcel in my function.
The problem is rather that the interface contract from the base class says
that you will accept any FStorage. You're now only providing an
implementation for the subset of FStorage objects which as FSExcel-derived.
This breaks the contract, and the compiler tells you so.
Jun 27 '08 #10

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

Similar topics

4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
1
by: Tony Johansson | last post by:
Hello! Private inheritance is sometimes called implementation inheritance. If you use this private inheritance how is with the usage of overriding then. Is overriding used less often when...
0
by: Ian | last post by:
I've got this problem with overriding attributes on an inheritance chain where the XmlSerializer is concerned. For example: public class A { private string aWord = String.Empty(); public...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
2
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which...
6
by: tshad | last post by:
I am playing with Inheritance and want to make sure I understand it. I have the following Classes: ******************************************* Public Class AuthHeader:Inherits SoapHeader Public...
4
by: vivekian | last post by:
Hi, Have this following hierarchy which am implementing for a networking program. The base class 'ASocket' is the base class from which 'AListener' and 'ATalker' inherit . None of the functions...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
6
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.