471,330 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 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 1258
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dan Bullok | last post: by
1 post views Thread by Tony Johansson | last post: by
45 posts views Thread by Ben Blank | last post: by
7 posts views Thread by Hazz | last post: by
2 posts views Thread by Emmanuel | last post: by
6 posts views Thread by tshad | last post: by
4 posts views Thread by vivekian | last post: by
11 posts views Thread by John | last post: by
6 posts views Thread by karthikbalaguru | last post: by
reply views Thread by rosydwin | last post: by

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.