By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,035 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

abstract, virtual and inheritance

P: n/a
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) ?

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Magne Ryholt <ma**********@bluezone.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<snip>
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
Nov 16 '05 #3

P: n/a
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)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Magne Ryholt <ma**********@bluezone.no> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.