0

In the below example, I want to replace part of a calculation without having to re-implement the entire calculation in the derived sub classes.

class DummyCalcBase
{
    public int changeable_part()
    {
        return 5;
    }

    public int common_calculation()
    {
        return 5 * changeable_part();
    }
}

class DummyCalc : DummyCalcBase
{
    public new int changeable_part()
    {
        return 10;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int c = new DummyCalcBase().common_calculation();
        Console.WriteLine("Base gives " + c.ToString());

        int c2 = new DummyCalc().common_calculation();
        Console.WriteLine("Calc gives " + c2.ToString());
    }
}

This then gives output: Base gives 25 Calc gives 25

What I would like is to get DummyCalc().common_calculation() to call the new changeable_part (and give the answer 50).

This would mean I don't have to copy and paste the same method into sub classes.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
GarethD
  • 112
  • 1
  • 8
  • 4
    `changeable_part` in `DummyCalcBase` should be `virtual`, and be `override`n in `DummyCalc`. – Bart van Nierop Apr 14 '15 at 14:54
  • 2
    As an aside, even more striking is it to have a base reference to a derived and exploit polymorphism (after honing the virtual/override advice of the others): `DummyCalcBase dcb = new DummyCalc(); Console.WriteLine(dcb.common_calculation());` should print 50, even though dcb is a of type "reference of DummyCalcBase". – Peter - Reinstate Monica Apr 14 '15 at 14:57
  • You've shadowed the method not overridden it. – Jodrell Apr 14 '15 at 15:57

3 Answers3

11

you can override method if it is virtual

class DummyCalcBase
{
    public virtual int changeable_part()
    {
        return 5;
    }

    public int common_calculation()
    {
        return 5 * changeable_part();
    }
}

class DummyCalc : DummyCalcBase
{
    public override int changeable_part()
    {
        return 10;
    }
}

methods with new keyword only hide methods of base class

if method is virtual, the following code will compute 50:

DummyCalcBase dummy = new DummyCalc();
int calc = dummy.common_calculation();

SO: new vs override difference

Community
  • 1
  • 1
ASh
  • 34,632
  • 9
  • 60
  • 82
4

Mark the method in the base class as either virtual if you want to provide a default implementation that derived types can override, or abstract if you want to leave the implementation up to derived types.

Then simply override those methods in your derived types, and provide functionality as needed.

aevitas
  • 3,753
  • 2
  • 28
  • 39
0

If your scenario is as simple as you describe here, go for the virtual method, with an override method in the sub-class. If your calculation is more complex, you should take a look at the strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern

Your code will than look something like this:

public interface IStrategy
{
    int getValue();
}

public class Context
{
    private readonly IStrategy strategy;

    public Context(IStrategy strategy)
    {
       this.strategy = strategy;
    }

    public int common_calculation()
    {
        return 5 * strategy.getValue();
    }
}

public class FiveStrategy : IStrategy
{
     public int getValue()
     {
         return 5;
     }
}

public class TenStrategy : IStrategy
{
    public int getValue()
    {
        return 10;
    }
}

internal class Program
{
    public static void Main(string[] args)
    {
        var context5 = new Context(new FiveStrategy());
        Console.WriteLine(context5.common_calculation());

        var context10 = new Context(new TenStrategy());
        Console.WriteLine(context10.common_calculation());

        Console.ReadLine();
    }
}
Pieter
  • 103
  • 4