Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An abstract method overrides an abstract method

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?

like image 934
Alon Gubkin Avatar asked Nov 20 '09 07:11

Alon Gubkin


People also ask

How do you override an abstract?

A non-abstract child class of an abstract parent class must override each of the abstract methods of its parent. A non-abstract child must override each abstract method inherited from its parent by defining a method with the same signature and same return type. Objects of the child class will include this method.

Are abstract methods intended to be overridden?

Abstract methods cannot be overridden by a concrete subclass.

Is it possible to override non-abstract method as a abstract method?

An abstract class can have a mixture of abstract and non-abstract methods. Subclasses of an abstract class must implement (override) all abstract methods of its abstract superclass. The non-abstract methods of the superclass are just inherited as they are. They can also be overridden, if needed.

Can we override abstract method in interface?

An abstract class can override Object class methods, but an interface can't.


4 Answers

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.

like image 119
Razzie Avatar answered Nov 13 '22 07:11

Razzie


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");
    }
}
like image 8
Russell Mull Avatar answered Nov 13 '22 07:11

Russell Mull


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()";
    }
}
like image 4
Sam Harwell Avatar answered Nov 13 '22 08:11

Sam Harwell


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

like image 3
Mark Bertenshaw Avatar answered Nov 13 '22 08:11

Mark Bertenshaw