abstract, virtual and inheritance

  • Thread starter Thread starter Magne Ryholt
  • Start date Start date
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) ?
 
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.
 
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
 
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)
 
Back
Top