POLYMORPHISM
Polymorphism in programming is a concept that allows code to be written in a way that objects of different types can be treated in the same way. In other words, an object might take on various forms or have a single name that serves several purposes.
This means that a single piece of code can be used to perform different tasks depending on the type of object it is applied to. Polymorphism is a powerful tool for writing more efficient and easier-to-understand code.
There are two(2) types of polymorphism which are: Static/ Compile Time and Dynamic/ Runtime polymorphism.
Static / Compile Time:
This is a type of polymorphism that the response to a method is determined at compile time. This indicates that the code is already known at compile time and that the execution of the code is unaffected by the object type or parameters passed to the method/function.
In an object-oriented language like c#, for instance, the response to a method call is determined at compile time when overriding it. It is also known as Early Binding.
There are two types of static polymorphism which are: Method overloading and operator overloading. The idea of method overloading is when the names of more than one method are the same but they have different parameters.
At compile time the compiler checks the type and number of parameters declared/passed, then decides on which method to call and it throws an error if no method match is found.
public class StaticPolymorphism
{
public int Add(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
public int Add(int num1, int num2)
{
return num1 + num2;
}
}
class Program
{
static void Main(string[] args)
{
StaticPolymorphism MethodOverloading = new StaticPolymorphism();
int addFirstMethod = MethodOverloading.Add(55, 45, 35);
int addSecondMethod = MethodOverloading.Add(25, 15);
}
} In the above code block, the class contains two(2) methods called “Add” which have three(3) and two(2) parameters respectively.
Get Johnson’s stories in your inbox
Join Medium for free to get updates from this writer.
However, in C#, operator overloading is the process of redefining an operator’s meaning to make it work with user-defined data types. With this, objects, structs, classes, and other types of data can be handled with the same operator.
Similar to how it is used with primitive types, operator overloading is used to perform operations on user-defined types like classes and structs.
public class OperatorOverloading
{
public static void AddNumbers(int num1, int num2)
{
int addNumber = num1 + num2;
Console.WriteLine("Sum of numbers is = {0}",addNumber);
}
public static void AddString(string str1, string str2)
{
string AddString = str1 + str2;
Console.WriteLine("Sum of stringss is = {0}",AddString);
}
public static void Run()
{
OperatorOverloading.AddNumbers(1, 2);
OperatorOverloading.AddString("1", "2");
}
}
//Result
//Sum of numbers is = 3
//Sum of strings is = 12Depending on the types of data that are provided (from the above example’s case, strings, and integers), the + operator performs specific operations. When the + operator is applied to mathematical sorts, the outcome is the summation of the operands. String concatenation, on the other hand, occurs when string types are given the + operator.
Dynamic/ Runtime Polymorphism:
When a call to a member function is resolved at runtime rather than compile time, dynamic polymorphism takes place. The type and values of the arguments passed to the function are used at runtime to determine the function’s call in dynamic polymorphism.
In C# is the ability of a method, property, or field to be overridden or redefined in a derived class. This allows various implementations of the same method/function, property, or field to be utilized relying upon the runtime sort of the object. This is also known as late binding or runtime binding.
class DynamicPolymorphism
{
public class Shape
{
protected const double PI = Math.PI;
protected static double _x,_y;
public Shape() { }
public Shape(double x, double y)
{
_x = x;
_y = y;
}
public virtual double Area()
{
return _x * _y;
}
}
public class Circle : Shape
{
public Circle(double r) : base(r, 0)
{
}
public override double Area()
{
return PI * _x * _x;
}
}
public class Square : Shape
{
public Square(double r) : base(r, 0)
{
}
public override double Area()
{
return _x*_x;
}
}
public class Sphere : Shape
{
public Sphere(double r) : base(r, 0)
{
}
public override double Area()
{
return 4 * PI *_x * _x;
}
}
public class Cylinder:Shape
{
public Cylinder(double r, double h) : base(r, h)
{
}
public override double Area()
{
return 2 *PI*_x* _x + 2 * PI * _x * _y;
}
}
public static void Main()
{
double r = 3.0, h = 5.0; //r is the radius, h is the height
Shape area = new Circle(r);
Shape areaOfSquare = new Square(r);
Shape areaOfSphere = new Sphere(r);
Shape areaOfCylinder = new Sphere(r,h);
Console.WriteLine("Area of a circle is {0}", area.Area());
Console.WriteLine("Area of a square is {0}", areaOfSquare.Area());
Console.WriteLine("Area of a sphere is {0}", areaOfSphere.Area());
Console.WriteLine("Area of a cylinder is {0}", areaOfCylinder.Area());
}
}
/*
Output:
Area of Circle = 28.27
Area of Square = 9
Area of Sphere = 113.10
Area of Cylinder = 150.80
}*/Although the compiler successfully compiles despite requiring an Area() method, the appropriate version is determined at runtime rather than compile time.
Here is another example using Abstract classes:
public abstract class Shape
{
protected const double PI = Math.PI;
protected static double _x, _y;
public abstract void Area();
}
public class Circle : Shape
{
public Circle(double radius) => _x = radius;
public override void Area()
{
var area = PI * _x * _x;
Console.WriteLine("Area of a circle is {0}", area);
}
}
public class Sphere : Shape
{
public Sphere(double radius) => _x = x;
public override void Area()
{
var area = 4 * PI * _x * _x;
Console.WriteLine("Area of a sphere is {0}", area);
}
}
public class Cylinder : Shape
{
public Cylinder(double x, double y)
{
_x = x;
_y = y;
}
public override void Area()
{
var area = 2 * PI * _x * _x + 2 * PI * _x * _y;
Console.WriteLine("Area of a cylinder is {0}", area);
}
}
public abstract class Example
{
public static void Run()
{
var Radius = 4;
var Height = 5;
Shape AreaOfCirclecircle = new Circle(Radius);
AreaOfCirclecircle.Area();
Console.WriteLine();
Shape AreaOfSphere = new Sphere(Radius);
AreaOfSphere.Area();
Console.WriteLine();
Shape AreaOfCylinder = new Cylinder(Radius,Height);
AreaOfCylinder.Area();
}
//Area of circle is 50.2654824574367
//Area of sphere is 201.061929829747
//Area of cylinder is 226.194671058465
}Finally, the virtual or abstract method that is overridden in the derived class must have the same name, number of parameters, and type as the base class method’s virtual or abstract method.







