Abstract or virtual method?
Whats recommended in an abstract class?
Have a virtual method returning null, or have an abstract method?
It is convenient and easy to have them returning null, as I don't have开发者_Go百科 to do anything inheriting from the abstract class. But still, returning null is somewhat hard to understand if it is by purpose, error, or by design....
Are there any clear recommendations for this?
If returning null is a sensible and sane thing to do as a default behaviour (with supporting documentation), then perhaps virtual
is fine. Likewise, if you can create a sensible default but just want to offer extensibility: virtual
.
If the (concrete) class cannot function without a non-default implementation, then make it abstract
. The casebook scenario here is Stream
: without a sane implementation where would the bytes come from? (/go to).
I have an abstract method if I demand the derived class provide functionality, and a virtual method returning null (or doing some default action) if I'm not so bothered.
精彩评论