abstract, virtual and inheritance

M

Magne Ryholt

I have a base class and a chain of derived classes.
The base and its derived classes are all abstract except the last in chain
(a concrete class).
I want to provide some functionalities in the base class by defining a
member as e.g. "protected virtual MyReturnClass MyMethod(){...}"
The base classes, including the last (concrete class) adds on this
functionality by defining members as e.g. "protected override MyReturnClass
MyMethod(){... return base.MyMethod();}"

This works fine, however I often forget to implement MyMethod in the classes
(both abstract and the concrete class(es), except (obviously) the "top
base").
Of course this is found during testing, but I would like the compiler to, at
least warn me that "my intention is not fulfilled" (better yet with an
error).
Abstract member declarations would enforce this, but abstract members cannot
contain any body, so I am looking for something like a special abstract
modifier which accepts code-bodies (I know this does not exist).

Is there a way to force overriding by combining some of the modifiers or by
other means (e.g. interfaces etc.) ?
What if the "chained functionalites" needs to be bodies of constructors (as
it is in my actual case) ?
 
J

Jon Skeet [C# MVP]

Magne Ryholt said:
I have a base class and a chain of derived classes.
The base and its derived classes are all abstract except the last in chain
(a concrete class).
I want to provide some functionalities in the base class by defining a
member as e.g. "protected virtual MyReturnClass MyMethod(){...}"
The base classes, including the last (concrete class) adds on this
functionality by defining members as e.g. "protected override MyReturnClass
MyMethod(){... return base.MyMethod();}"

This works fine, however I often forget to implement MyMethod in the classes
(both abstract and the concrete class(es), except (obviously) the "top
base").
Of course this is found during testing, but I would like the compiler to, at
least warn me that "my intention is not fulfilled" (better yet with an
error).
Abstract member declarations would enforce this, but abstract members cannot
contain any body, so I am looking for something like a special abstract
modifier which accepts code-bodies (I know this does not exist).

Is there a way to force overriding by combining some of the modifiers or by
other means (e.g. interfaces etc.) ?
What if the "chained functionalites" needs to be bodies of constructors (as
it is in my actual case) ?

Define an abstract member and provide a similarly named concrete
member, eg DoSomething and DoSomethingBaseImpl. Your derived classes
would then call DoSomethingBaseImpl in their implementations of
DoSomething.
 
D

Daniel Pratt

Define an abstract member and provide a similarly named concrete
member, eg DoSomething and DoSomethingBaseImpl. Your derived classes
would then call DoSomethingBaseImpl in their implementations of
DoSomething.

A slight variation of Jon's suggestion would be to have a concrete,
non-virtual method in the abstract class that calls a protected, abstract
method in the abstract class. Something like:

// in the abstract class
...
public void DoSomething()
{
...
AfterDoSomething();
...
}

protected abstract void AfterDoSomething();
...

// in the concrete class
...
protected override void AfterDoSomething()
{
...
}

The advantage to doing this is that it does not require the inheriting class
to call the base class method.

Regards,
Daniel
 
M

Magne Ryholt

Thanks to Jon and Daniel

Do you know some short and good explanation (or a url) to why abstract
methods should not be allowed to have implementation in the class it is
declared ?
I have certainly had the need to implement and enforce derived class
overriding (but because it is not possible I have not seen potential
problems with it)
Would it break "good OO practice etc. ? polymorphism ?
Perhaps the word abstract would not be the correct one anymore (I fully
understand the meaning of abstract regarding classes, regarding methods or
properties the word is a bit more "fuzzy" to me, but my mother-tongue is not
english :).
I have seen that C# 2.0 introduces a new keyword, namely partial, perhaps
this word would be better ? (C# includes different interpretations of the
same keyword anyway e.g. new)
 

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