0

Obviously using virtual and override is the normal situation, but does this telecoms'ish example count?

public class Pipe
{
  // whole bunch of protected member variables such as bandwidth, latency, download limit 
  // etc,

  public int GetCost()
  {
     // work out cost based on above
  }
}

public class BigFatPipe : Pipe
{
  public BigFatPipe()
  {
    // sets up the member variables one way
  }
}

public class CheapestPossiblePipe: Pipe
{
  public CheapestPossiblePipe()
  {
    // sets up the member variables another way
  }
}

then you might call

PrintPrice(new BigFatPipe())

PrintPrice(new CheapestPossiblePipe())

public void PrintPrice(Pipe pipe)
{
   int a = pipe.GetCost();
   ....
}

You'll get two different answers. This isn't the most useful example but does it count?

tony
  • 2,178
  • 2
  • 23
  • 40
  • Is it a C++ question or a C# one? You should only use tags relevant to your question. – Nasreddine May 27 '16 at 07:31
  • 2
    Polymorphism is the ability of an object to take many forms. It does not have to override a method per se'. – Mohamad Elghawi May 27 '16 at 07:31
  • It's cross language, I could have included Java as well and possibly others. Polymorphism wasn't a tag that I thought would get much attention – tony May 27 '16 at 07:35

4 Answers4

0

This post here has a useful discussion of what exactly polymorphism is.

I think most definitions do not explicitly state that an object must have virtual functions to be polymorphic - so yes, I think your example counts.

Community
  • 1
  • 1
Smeeheey
  • 9,906
  • 23
  • 39
0

Constructor overloading is a recognized method to implement static polymorphism. While this isn't really constructor overloading, it's close. So yes, I'd call it polymorphism.

Ash
  • 5,786
  • 5
  • 22
  • 42
0

This pattern does work, but introducing a bunch of classes will confuse the user uselessly: they will wonder what the classes do differently.

A few factories methods will do the same job and will be easier to understand and maintain:

public class Pipe
{
  // whole bunch of private member variables such as bandwidth, latency, download limit 
  // etc,

  public int GetCost()
  {
     // work out cost based on above
  }

  public static Pipe MakeBigFatPipe()
  {
      var result = new Pipe();
      // sets up the member variables one way
      return result;
  }

  public static Pipe MakeCheapestPossiblePipe()
  {
      var result = new Pipe();
      // sets up the member variables another way
      return result;
  }
}
Falanwe
  • 4,636
  • 22
  • 37
-1

If I were you I would use folowing approach:

public interface IGetCost
{
    int GetCost();
}

public class Pipe : IGetCost
{
    public int GetCost(){}
}

public class BigFatPipe : IGetCost
{
    //aggregation
    private readonly Pipe _pipe;

    public BigFatPipe(Pipe pipe)
    {
        _pipe = pipe;
    }
    public int GetCost() { }
}

public class CheapestPossiblePipe : IGetCost
{
    private readonly Pipe _pipe;

    public CheapestPossiblePipe(Pipe pipe)
    {
        _pipe = pipe;
    }
    public int GetCost() { }
}

public static void PrintPrice(IGetCost obj)
{
    int cost = obj.GetCost();
    Console.WriteLine(cost);
}

static void Main(string[] args)
{
    IGetCost p;

    p = new Pipe();
    PrintPrice(p);

    p = new BigFatPipe();
    PrintPrice(p);

    p = new CheapestPossiblePipe();
    PrintPrice(p);
}

I also need to say that there're two different things - polymorphism and overloading

polymorphism

public class foo
{
    public virtual void foo1{/*....*/}
}

public class fooA : foo
{
    public override void foo1{/*....*/}
}

public class fooB : foo
{
    public new void foo1{/*....*/}
}

public class fooC : foo
{
    //new is the default modifier
    public void foo1{/*....*/}
}

overloading

public class foo{
    public int foo1{/*....*/}
    public int foo1(int a){/*....*/}
    public int foo1(string a){/*....*/}
    public int foo1(int a, string b){/*....*/}
}
isxaker
  • 8,446
  • 12
  • 60
  • 87