Here is some code from MSDN:
// compile with: /target:library
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
Can anyone explain the above code with respect to the differences between abstract and virtual methods?
DoWork()
method is optional for all it's immediate-children/subclasses. The code above adds an intermediary abstract Class-E to force the overriding of Class-D'sDoWork()
method for all subclasses of Class-E - essentially preventing all of Class-E's children from using Grandpa-Class-D'sDoWork()
. In other words: the implementation of Class-D'sDoWork()
is hidden from Class-F; and it is now compulsory for Class-F (and all it's sibling classes) to each form their own custom implementation ofDoWork()
. – Sliest