2

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?

Spook
  • 25,318
  • 18
  • 90
  • 167

2 Answers2

6

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.

Spook
  • 25,318
  • 18
  • 90
  • 167
  • 1
    The answer was posted at **the same** time as the question?!! – Ahmed KRAIEM Oct 16 '13 at 13:09
  • 3
    @AhmedKRAIEM That's perfectly ok. In fact, when you ask a question there's even a specific option to "answer your own question". Try it - click "Ask a question" and look near the bottom of the page. – Matthew Watson Oct 16 '13 at 13:10
  • 1
    @MatthewWatson, Sorry I didn't know that. (and I'm on SO for more than 3 years). – Ahmed KRAIEM Oct 16 '13 at 13:11
  • 1
    @AhmedKRAIEM, whilst answering your own questions is fine, posting duplicate questions isn't :) Voted to close... – David Arno Oct 16 '13 at 13:15
  • 1
    @DavidArno It's not really duplicate since the two questions pretty much answer each others. – Ahmed KRAIEM Oct 16 '13 at 13:18
  • Interesting that C# allows that. Is there any way to convince it to allow one to both override and shadow a member without having to define an "intermediate" derived type? For example, if a type declares `public abstract int foo{get;}`, can a derived type implement that member and declare an `int foo{get;put;}`, or is the only way to make that work to have the base type use `public int foo {get {return getFoo();} protected abstract int getFoo();`? – supercat Nov 14 '13 at 16:34
-2

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()
   {

   }

}
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
  • -1 There is a better way. And it already exists as an answer. Shadowing is almost never a good idea. – Daniel Hilgarth Oct 16 '13 at 13:19
  • really???...hummmm ok i guess msdn saying its the default behavior when implementing your own in a derived class is wrong...."almost never" does not mean every time... – terrybozzio Oct 16 '13 at 13:20
  • That does not solve the problem either. Someone, who derives from `Derived2` won't easily know, that he *must* override `DoSth`. – Spook Oct 16 '13 at 17:56