views:

215

answers:

6
public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
    public abstract override void Process();
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}

This code throws an Compilation Error: 'B' does not implement inherited abstract member 'A.Process()'.

Is there any way to do this?

A: 

Are you sure? Which version of C#.

I compiled and all seems fine.

astander
+10  A: 

Just leave out the method completely in class B. B inherits it anyway from A, and since B itself is abstract, you do not explicitly need to implement it again.

Razzie
+5  A: 

Works for me in VS2008; no errors, no warnings. BUT, there's no reason to have the 'override' in B. This code is equivalent:

public abstract class A
{
    public abstract void Process();
}

public abstract class B : A
{
}

public class C : B
{
    public override void Process()
    {
        Console.WriteLine("abc");
    }
}
Russell Mull
+2  A: 

Alon -

This makes no sense. First of all, this does actually compile fine. Secondly, the abstract method you declared in A is inherited (still abstract) into B. Therefore, you have no need to declare Process() in class B.

-- Mark

Mark Bertenshaw
+2  A: 

The place where I've seen this sometimes is overriding a non-abstract virtual method with an abstract method. For example:

public abstract class SomeBaseType
{
    /* Override the ToString method inherited from Object with an abstract
     * method. Non-abstract types derived from SomeBaseType will have to provide
     * their own implementation of ToString() to override Object.ToString().
     */
    public abstract override string ToString();
}

public class SomeType : SomeBaseType
{
    public override string ToString()
    {
        return "This type *must* implement an override of ToString()";
    }
}
280Z28
I think I know what you mean but your example doesn't reflect what you are saying. At least you should add a SomeVeryBaseType class with the non-abstract virtual method and let SomeBaseType inherit from SomVeryBaseType.
Lieven
For those dummies who don't know or get that... like me
Lieven
@Lieven: I added a comment to make it clear that the base method being overridden here is `Object.ToString()`.
280Z28
+2  A: 

Overriding makes no sense without an implementation.

Bedwyr Humphreys