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.
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;- Method abstraction, then the class also need to be abstract
- 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 JsonWriterUML 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!
Good blog,Great experience for me by reading this article.
ReplyDeletecore java training in chennai
core java training
core java Training in Porur
C++ Training in Chennai
javascript training in chennai
Appium Training in Chennai
JMeter Training in Chennai