2

Asuming I have this two classes:

public class Threshold {
    int min;
    int max;

    public virtual int calculate(int min, int max)
    {
        //calculate basic Threshold        
    }        
}


public class AdvancedThreshold : Threshold {
    int min;
    int max;
    int extra;

    public override int calculate(int min, int max, int extra)
    {
        //calculate Advanced Threshold        
    }
}

I cannot do this, because the base method has two parameters whereas the subclass method has three parameters.

What would be the best design practice to approach this problem? is it possible to use polymorphic objects? do I need to use composite objects instead of inheritance?

I've read something similar to my problem here https://stackoverflow.com/a/56904/3032175

But I don't really understand the solution. Thank you.

Community
  • 1
  • 1
user3032175
  • 87
  • 1
  • 7
  • 1
    I don't understand the problem; is the problem that an AdvancedThreshold object's method must *not* be called with two arguments? Inheritance does not deal well with adding *restrictions* to a subclass; subclasses are supposed to add new capabilities, not new restrictions. Perhaps you have your class relationship backwards? – Eric Lippert Sep 19 '15 at 17:56
  • I am trying to solve this problem http://stackoverflow.com/a/56904/3032175 the board and threeDboard – user3032175 Sep 20 '15 at 08:37

2 Answers2

1

When you inherit from an class you cannot change the methods you need to provide implementations for the methods.However you can add new methods.

There are few ways to achieve what you want

you provide two overloads for the method

public virtual int calculate(int min, int max){}

public virtual int calculate(int min, int max,int extra){}

or you can implement one in terms of another

public override int calculate(int min, int max)
{
   calculate(min,max,extra)
}

public  int calculate(int min, int max, int extra){}
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • Yes I understand that. What I am trying to ask is what is the best solution to this problem? I cannot use inheritance. Even if I use it by writing a new method I am violating LISKOV principle. So what would be the best solution? – user3032175 Sep 19 '15 at 17:08
  • Thanks. What about this post http://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle/56904#56904 Maybe we should find another approach. Instead of extending Board, ThreeDBoard should be composed of Board objects. One Board object per unit of the Z axis. I don't really understand what is he refering to – user3032175 Sep 19 '15 at 17:14
1

Not exactly sure what you're trying to achieve, but you might want to rethink the design. It seems that the min/max/extra values should not be passed to the Calculate method but set as properties or passed in a constructor.

public class Threshold
{
    public int Min { get; set; }
    public int Max { get; set; }

    public virtual int Calculate( int input )
    {
        // logic goes here
    }
}

public class AdvancedThreshold : Threshold
{
    public int Extra { get; set; }

    public override int Calculate( int input )
    {
        // advanced logic goes here
    }
}
Chris
  • 5,442
  • 17
  • 30
  • Yes this is what I thought. Maybe a calculcate method without any parameters and pass the parameters to the constructor. What I am trying to solve is this problem http://stackoverflow.com/a/56904/3032175 with the best design approach – user3032175 Sep 20 '15 at 08:36
  • @user3032175 Yeah the board/3d board problem is certainly not as straight forward as this one. – Chris Sep 21 '15 at 17:28