Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

polymorphism

polymorphism

  • Please log in to reply
13 replies to this topic

#13 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 20 March 2009 - 12:33 PM

Hello ArekBulski,

trust me everyone gets lazy, cutting corners to finish something, I know I have here and there, but I actually envy you all that knows multiple computer languages which I hope to accomplish sometime next year, but we will see what happens...

ANYWAY... Last question and this time I will end this post with this, this keyword, can anyone give me an example (simple code will due) and explanation why you would use this keyword?

Thanks
  • 0

#14 BillDoorNZ

BillDoorNZ

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 24 March 2009 - 01:57 AM

Hello,

Wondering if someone can easily explain polymorphism as well as show a very simple/basic exmaple of it below, I am looking at some of the examples online and it sorta makes me wonder, why do I need it, inheritance pretty much gives you the chance to work with both classes even though you initiated one (initiated the child class that is...). so again breifly english explanation and example might help me 'click' in the brain : )

Thank you all


Hey Siten,

if you are using inheritence, you are half-way to using polymorphism already ;)

Polymorphism is really quite simple, but it can be confusing to get your head around it at first. I'll give you an example that uses it. I'm writing a drawing program that draws shapes on a form / screen and calculates the area contained within the shape (no idea why as this is imaginary and contrived) ;)

public abstract class Shape
{
  protected Point _center;

  public abstract decimal CalculateArea();
  public abstract void Draw(Point location);
  public virtual void Draw()
  {
     Draw(_center);
  }
}

We then derive a Rectangle shape:

public class Rectangle: Shape
{
  public int width;
  public int height;
 
  //constructor for when i don't know where to draw it yet
  public Rectangle()
  {
  }

  //constructor for when i know where to draw the shape
  public Rectangle(Point _startLocation, int width, int height)
  {
    _center = _startLocation;
   this.width = width;
   this.height = height;
  }

  public override decimal CalculateArea()
  {
     return (width * height);
  }

  public override void Draw(Point location)
  {
    //draw a rectangle at location Point.X, Point.Y
    //that is 'width' wide and 'height' high
  }
}

And now a circle:

public class Circle: Shape
{
  public int radius;
 
  //constructor for when i don't know where to draw it yet
  public Rectangle()
  {
  }

  //constructor for when i know where to draw the shape
  public Rectangle(Point _startLocation, int radius)
  {
    _center = _startLocation;
   this.radius = radius;
  }

  public override decimal CalculateArea()
  {
     return (Math.PI * radius * radius);
  }

  public override void Draw(Point location)
  {
    //draw a circle at location Point.X, Point.Y
  }
}

So....now we have our two shapes we can draw....tho we'd also have all sorts of other shapes too (triangles, octogons, pentagons etc)....so, we give the user a nice shiny form with a toolbox of shapes that they drag onto the form and drop....where do we store the references to these shapes tho? why...in a list of course!! but what sort of list? do we create a list like this:

...
  private List<object> _shapes = new List<object>();
...

and the have to cast each item to the appropriate shape?

...
  public decimal GetTotalAreaInAllShapes()
  {
    decimal result = 0;
    foreach(object obj in _shapes)
    {
       if (obj is Rectangle) result += (obj as Rectangle).CalculateArea();
       if (obj is Circle) result += (obj as Circle).CalculateArea();
    }
    return result;
  }



WTF!!! no way...we do this instead:

...
  private List<Shape>_shapes = new List<Shape>();
...
...
  public decimal GetTotalAreaInAllShapes()
  {
    decimal result = 0;
    foreach(Shape shape in _shapes)
    {
       result += shape.CalculateArea();
    }
    return result;
  }


Ideally you would also pass the _shapes list into the function to make it more generic...but thats another whole story there (refactoring).

So, as you can see, polymorphism just lets you treat a whole heap of descendant classes the same way (via the Shape class interface)

This can also be done withe an interface. e.g.

public interface Shape
{
  decimal CalculateArea();
  void Draw(Point location);
  void Draw();
}

public class Rectangle: IShape
{
  public int width;
  public int height;
  public Point _center;
 
  //constructor for when i don't know where to draw it yet
  public Rectangle()
  {
  }

  //constructor for when i know where to draw the shape
  public Rectangle(Point _startLocation, int width, int height)
  {
    _center = _startLocation;
   this.width = width;
   this.height = height;
  }

  public override decimal CalculateArea()
  {
     return (width * height);
  }

  public override void Draw(Point location)
  {
    //draw a rectangle at location Point.X, Point.Y
    //that is 'width' wide and 'height' high
  }

  public override void Draw()
  {
    Draw(_center);
  }
}

  • 0





Also tagged with one or more of these keywords: polymorphism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download