views:

33

answers:

2

Consider this piece of code:

public abstract class Validator
{

    protected Validator()
    {
    }


    protected abstract void ValidateCore(object instance, string value, IList<ValidationResult> results);


    public void Validate(object instance, string value, IList<ValidationResult> results)
    {
        if (null == instance) throw new ArgumentNullException("instance");
        if (null == results) throw new ArgumentNullException("results");

        ValidateCore(instance, value, results);
    }
}

Look at the Validate() overload, how can an abstract class have definitions like this?

A: 

I think it is perfectly normal. That's called Template Method pattern. Right?

http://en.wikipedia.org/wiki/Template_method_pattern

Lex Li
Thanks Lex, I am going through it. I am learning my way up
Soham
+2  A: 

An abstract class should have at least one abstract method. It does not mean it can't define concrete methods. One of the usages of this property is the Template Method design pattern which lets you define an algorithm in a way that it can be changed by subclasses.

Ikaso
Yes - abstract methods are not interfaces.
Jeremy McGee
In fact an abstract class can have no abstract method (you can test it out), though that's not a usual way to define abstract classes.
Lex Li
hmm... interesting, thanks!
Soham