2

I'm having a base class defined in my app

Something like,

using System;

public class BaseClass
    {
       public virtual void Method2() {Console.WriteLine("base2");}
       public virtual void Method1() {Console.WriteLine("base1");}
    }
public class Derived1 : BaseClass
    {
       public override void Method2() {Console.WriteLine("derived1-2");}
       public override void Method1() {Console.WriteLine("derived1-1");}
    }
    public class Derived2 : Derived1
    {
       public override void Method1() {Console.WriteLine("derived2-2");}


    }
public class Program
{
    public static void Main()
    {
        var obj = new Derived2();
        ((BaseClass)obj).Method2();
        Console.WriteLine("Hello World");
    }
}

Say I need to access the Method2() of my very first BaseClass. Even after typecasting it to ((BaseClass)obj).Method2(), I'm still getting derived1-2 while I'm expecting base2'.

How do I do that ?

  • 3
    `BaseClass` was (apparently) designed so that calls to `Method2` resolve to the most specific type which overrides that method. If you don't want that to happen, you need to redesign your base classes or hierarchy. It could be done in `IL` (`call` vs `callvirt`) but not expressible in C# – Damien_The_Unbeliever Nov 19 '18 at 14:08
  • https://stackoverflow.com/questions/8329470/convert-derived-class-to-base-class – Jonesopolis Nov 19 '18 at 14:08
  • 7
    You don't. That's what `virtual` is all about. If you want the method to remain accessible as-is to derived classes, provide a (`protected`) non-virtual implementation that the virtual method calls. – Jeroen Mostert Nov 19 '18 at 14:08
  • 1
    If you want a method to be determined by the variable's type and not the actual object's type then don't use `virtual` and then use `new` instead of `override`. – juharr Nov 19 '18 at 14:15
  • 2
    As a side-note to this conversation, having a class that overrides inherited functionality is risky. You should call the inherited class's method, then your extra code in your override. For example, perhaps the base class has some validation code to prevent bad calls, you'd be skipping that check if you completely override the method. Maybe I've been reading too much Bob Martin :( p.s. Maybe your base class should have had "Open for Extension" in mind when it was being created ^^ – Dan Rayson Nov 19 '18 at 14:20

1 Answers1

1

I completely agree with the comments: it sounds like the behaviour that you require would need you to re-think your classes - possibly inheritance is not the best solution in this case. It depends what you're trying to do; you could obviously just do this:

var obj = new BaseClass();
((BaseClass)obj).Method2();
Console.WriteLine("Hello World");

My guess would be that what you actually want here is some form of injection; for example:

public class BaseClass
{
    private readonly IFunctionality1 functionality1;

    public virtual void Method2() { Console.WriteLine("base2"); }
    public virtual void Method1() { this.functionality1.Method1(); }

    public BaseClass(IFunctionality1 functionality1)
    {
        this.functionality1 = functionality1;
    }
}
public class Derived1 : BaseClass
{
    public Derived1(IFunctionality1 functionality1) : base (functionality1)
    {

    }

}

In this case, you might find that inheritance isn't even required anymore.

Fildor
  • 14,510
  • 4
  • 35
  • 67
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269