1

I was working on a certain problem and found some interesting problem inside.

Let me explain with an example (C# code)

public class A: IA
{
    protected abstract void Append(LoggingEvent loggingEvent)
    { 
          //Some definition
    }
}


public class B: A
{
    protected override void Append(LoggingEvent loggingEvent)
    { 
          //override definition
    }
}



public class MyClass: B
{
    //Here I want to change the definition of Append method.
}

Class A and B are of a certain library and I don't have any control to change those classes.

Deepak
  • 1,510
  • 1
  • 14
  • 27
  • 3
    You can override again. Very simple to try and verify. – bommelding Jul 06 '18 at 10:51
  • 2
    @HimBromBeere - Append() _remains_ virtual until you seal it. – bommelding Jul 06 '18 at 10:51
  • 1
    Note that Append() in A cannot have code in it like your example. It's either an abstract method without an implementation or you can declare it as virtual if you want to put something there. – Adam G Jul 06 '18 at 11:15

2 Answers2

1

Since none of the methods in the hierarchy here are sealed, you can just continue overriding the method yourself:

public class MyClass: B
{
    protected override void Append(LoggingEvent loggingEvent)
    { 
          // New logic goes here...
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
-1

I have shared the solution below based as per my research, but made few following changes to the code you shared based on my perception, since the code in the question is not valid at few occasions.

  1. Added an empty Interface IA, as Class A is not implementing any public method.
  2. Defined Class A as abstract, as any non-abstract class cannot define a abstract method.
  3. Removed the body for Append method inside Class A, as a abstract method cannot have a body.

    public interface IA
    {
    
    }
    
    public abstract class A : IA
    {
        protected abstract void Append();
    
    }
    
    public class B : A
    {
        protected override void Append()
        {
            //override definition
        }
    }
    
    public class MyClass : B
    {
        //Here I want to change the definition of Append method.
    
        //You can do is hide the method by creating a new method with the same name
        public new void Append() { }
    }
    

Answer : You cannot override a non-virtual method. The closest thing you can do is hide the method by creating a new method with the same name but this is not advisable as it breaks good design principles. But even hiding a method won't give you execution time polymorphic dispatch of method calls like a true virtual method call would.

  • [Abstract methods are implicitly virtual.](https://stackoverflow.com/questions/391557/is-every-abstract-function-virtual-in-c-in-general) Method hiding is exactly what OP does *not* want to do. – BJ Myers Jul 06 '18 at 15:52
  • @BJMyers I agree with you completely. The question itself is not very much pinpoint. The above answer comes with possible corrections to the question and at the end of the Answer, I do highlight that Method Hiding is not recommended. Thanks for your help :) – Kamlesh Mj Jul 09 '18 at 08:18