Polymorphism

In last post I described Inhabitance, one of four major principles of object oriented programming. Today we will take a look at Polymorphism, second equally important principle.

Polymorphism (from Greek  polys, “many, much” and morphē, “form, shape”)  describes process of using once declared method in many forms. This action is possible thanks to incarcerate.  Polymorphism have two main kinds: dynamic and static. Static polymorphism is the mechanism of linking a function with an object during compile time. Most important thing about static polymorphism is Function Overloading

Function Overloading

This tool can be used to change the number and types of arguments. that are passed to the function. This extends the functionality of the method. Is commonly used in declaring a class constructors. Let’s take a look at example.

using System;
 class Print
 {
 void print(int i)
 {
 Console.WriteLine("Printing int: {0}", i);
 }
// Function can be overloaded with different argument.
 void print(double f)
 {
 Console.WriteLine("Printing float: {0}", f);
 }
//Function can be also overloaded with many arguments.
 void print(int i, string s)
 {
 Console.WriteLine("Printing int: {0} and string: {1}",i, s);
 }
 static void Main(string[] args)
 {
 Print newfunction = new Print();
 newfunction.print(5);
 newfunction.print(500.263);
 newfunction.print(5, "Hello C++");
 Console.ReadKey();
 }
 }

Body of the function can be different for each method, that’s  why it can be so useful to  improve the usability of the code.

Overriding 

Another type of Polymorphism is  overriding,  also called run-time polymorphism.

This action is performed by using key word override. It is used to change the body of the method declared in parent class. This way we can use method with exactly the same name for different purposes. In this simple example we will override method for writing a line of code.

using System;

public class Line : DrawingObject
{
 public override void Draw()
 {
 Console.WriteLine("I'm a Line.");
 }
}

public class Circle : DrawingObject
{
 public override void Draw()
 {
 Console.WriteLine("I'm a Circle.");
 }
}

public class Square : DrawingObject
{
 public override void Draw()
 {
 Console.WriteLine("I'm a Square.");
 }
}

This way we can loop between all Elements of the same parent class and have different results.

public class DrawDemo
{
 public static int Main()
 {
 DrawingObject[] dObj = new DrawingObject[4];

 dObj[0] = new Line();
 dObj[1] = new Circle();
 dObj[2] = new Square();
 dObj[3] = new DrawingObject();

//Loop that runs for every element of an array.
 foreach (DrawingObject drawObj in dObj)
 {
 drawObj.Draw();
 }
 return 0;
 }
}

In this post I covered basic use of Polymorphism, in the next post we will take brief look at another major concept called Encapsulation. Use this knowledge to experiment and to  build more flexible software and share you results in comments. Stay Awesome!

Advertisements

Author: indiedevart

Indie Games Developer

One thought on “Polymorphism”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s