Question Heading seems to be little confusing, But I will Try to clear my question here.
using System;
using System.Collections.Generic;
usi
I very much hope my answer will help some people confused by this issue.
Bear with me, but I'll try to re-summarise what is being asked, just to ensure that I am answering the right question. Then I'll give the answer!
I think the essence of the question is: How do I declare a method in an abstract class so that is has an implementation but still requires a derived class to override the method?
C# doesn't appear to support this. If you declare the method 'abstract', it is not permitted to have an implementation (body). But if you declare it 'virtual', a derived class is not forced to override it. C# deosn't allow a method to be marked as both abstract and virtual.
I believe there are many situations where you wish to do this (what is being asked).
The way to solve this riddle is as follows: Declare two methods! One of them is marked 'abstract'; the other is marked 'virtual' and calls the first one somewhere in its body.
This way, the derived class is forced to override the abstract method, but the (partial) implementation in the virtual method can be safely inherited.
So, for example, the Employee method GiveBonus might be declared thus:
public abstract decimal ComputeBonus();
public virtual void GiveBonus() {
decimal amount = ComputeBonus();
if (amount > 0.0) PostBonus(amount);
}
I'll leave the details of PostBonus to the imagination, but the beauty of this approach is that derived classes are forced to override ComputeBonus, yet GiveBonus benefits from the partial implementation provided in the base class. HTH