Polymorphism and Where to find them

I already describe what it is in the main OOP page but here is a great Video Explaining What is Polymorphism;

Polymorphism and Where to find them

In this section I'm attempt to explain Polymorphism in a different way. I'm going to show you the use case of Polymorphism an difference places listed below;

  • Polymorphism in Class
  • Polymorphism in Inheritance
  • Polymorphism in Composition

Polymorphism in Class

The most simplest use case, it goes by another name called Method Overloading.
here how it looks like;

public class Point
{
 int curX;
 int curY;
 public void Move(int x, int y) { }
 public void Move(Point pt) { }
}

you can see that method Move has multiple form, when calling the same method but with different signature it's actually called a different method.

Var myDot = new Point(0,0);
myDot.Move(3,4);
myDot.Move(new Point(3,4));

Polymorphism in Inheritance

In Inheritance we have Method Overriding. Method Overriding is the way to modify the implementation of an inherited method.
Keyword used for method overriding are;
  • Virtual
  • Override
public class Shape
{
 public virtual void Draw()
 {
  //Default Implementation
 }
}

public class Circle : Shape
{
 public override void Draw()
 {
  //New Implementation
 }
}

Now Draw from Class Shape and Class Circle will behave differently.
Actually the  class shape should be able to Draw at all cause will still don't know what shape to draw and We can use the abstract modifier to do so.

Abstract Modifier

Indicates that a class or a member is missing implementation. There are two level of abstraction;
  1. Method abstraction, then the class also need to be abstract
  2. Class abstraction, class method can be abstract or not abstract
public abstract class Shape
{
 //abstract method don't have Implementation
 public abstract void Draw();
 
}

public class Circle : Shape
{
 //all abstract class need to be override
 public override void Draw()
 {
  //Implementation for circle
 }
}

We use abstract when  we want to provide some common behavior, while forcing other developers to follow your design.

Polymorphism in Composition

When we want a composition to be polymophism we use something called Interface.
here's how interface look like

UML Representation

C# Implementation

public interface IWriter
{
    void WriteFile();
}
 
public class XmlWritter: IWriter
{
    public void WriteFile()
    {
        Console.WriteLine("Writing file in the XmlWriter class.");
    }
}
 
public class JsonWriter: IWriter
{
    public void WriteFile()
    {
        Console.WriteLine("Writing file in the JsonWritter class.");
    }
}

Using Interface

Now if we have a class that has a IWriter object inside it can be both XmlWritter or JsonWriter

UML Representation

here's FileWriter has a IWriter

C# Implementation

public class FileWriter
{
    private readonly IWriter _writer;
 
    public FileWriter(IWriter writer)
    {
        _writer = writer;
    }
 
    public void Write()
    {
        _writer.WriteFile();
    }
}

Show use case

class Program
{
    static void Main(string[] args)
    {
        XmlWriter xmlWriter = new XmlWriter();
        JsonWriter jsonWriter = new JsonWriter();
 
        FileWriter fileWriter = new FileWriter(xmlWriter);
        fileWriter.Write();
 
        fileWriter = new FileWriter(jsonWriter);
        fileWriter.Write();
 
        Console.ReadKey();
    }
}

Output

Writing file in the XmlWriter class.
Writing file in the JsonWritter class.


That should sum up the place where I think we can find the use case of polymorphism. If you think there other place that polymorphism can be used please feel free to comment below :)

happy coding every one! 

Deemarc Burakitbumrung

A software develper who graduated from Electronic and Communication Engineer. Have an interest in the field of programming and embedded system.

1 comment: