I’ve done this before – just can’t remember the trick.
If i have an abstract class:
public abstract class Post
And a set of deriving classes:
public class Photo : Post
I want to force the deriving classes to implement a method called Validate(), but at the same time providing core validation at the Post level.
I can create a method: public abstract void Validate() in Post, which would force the deriving classes to implement the method, but then how do i perform the Post (base) validation?
The end result is i want to be able to do this:
public class BLL
{
public void AddPost(Post post)
{
post.Validate(); // includes "Post" validation, any deriving validation.
repository.Add(post);
}
}
How can i do it?
Here is what you want:
This will force base classes to implement a validation technique, and the base Validate will get called by external users.
It is impossible to make a method abstract, and provide a default implementation.