Spook
Spook

Reputation: 25919

Re-abstracting overriden method

Let's look at the following class structure:

abstract class Base {

    public abstract void DoSth();
}

class Derived1 : Base {

    public override void DoSth() {

    }
}

These are base classes for some hierarchy. Now, let's assume, that we want to provide another class deriving from Derived1 (let's call it Derived2), which should not use the default implementation of DoSth provided by Default1. For example, Derived1 covers 98% of cases, but in the remaining 2%, this solution is not acceptable or dangerous.

The best solution is to notify someone who derives from Derived2, that he should implement DoSth during compilation. How to do that?

Upvotes: 2

Views: 77

Answers (2)

terrybozzio
terrybozzio

Reputation: 4532

Best way i can think of the top of my head is:

class Derived1 : Base
{

   public override void DoSth()
   {

   }
}

abstract class Derived2 : Derived1
{
   public virtual new void DoSth()
   {

   }

}

Upvotes: -2

Spook
Spook

Reputation: 25919

C# allows "re-abstracting" the method. One may write:

abstract class Derived2 : Derived1 {

    public abstract override void DoSth();
}

From now on, DoSth becames abstract again and compiler will refuse to compile class derived from Derived2 if it does not provide its own implementation of DoSth.

Upvotes: 6

Related Questions