Polymorphism

The word polymorph means many forms. To understand the concept of polymorphism properly, let's work with an example. Let's think about a person, such as Bill Gates. We all know that Bill Gates is a great software developer, businessman, philanthropist, and also a great human being. He is one individual, but he has different roles and performs different tasks. This is polymorphism. When Bill Gates was developing software, he was playing the role of a software developer. He was thinking about the code he was writing. Later, when he became the CEO of Microsoft, he started managing people and thinking about growing the business. He's the same person, but with different roles and different responsibilities.

In C#, there are two kind of polymorphism: static polymorphism and dynamic polymorphism. Static polymorphism is a kind of polymorphism where the role of a method is determined at compilation time, whereas, in dynamic polymorphism, the role of a method is determined at runtime. Examples of static polymorphism include method overloading and operator overloading. Let's take a look at an example of method overloading:

public class Calculator {
public int AddNumbers(int firstNum, int secondNum){
return firstNum + secondNum;
}

public double AddNumbers(double firstNum, double secondNum){
return firstNum + secondNum;
}
}

Here, we can see that we have two methods with the same name, AddNumbers. Normally, we can't have two methods that have the same name; however, as the parameters of those methods are different, methods are allowed to have the same name by the compiler. Writing a method with the same name as another method, but with different parameters, is called method overloading. This is a kind of polymorphism.

Like method overloading, operator overloading is also a static polymorphism. Let's look at an example of operator overloading to demonstrate this:

public class MyCalc
{
public int a;
public int b;

public MyCalc(int a, int b)
{
this.a = a;
this.b = b;
}

public static MyCalc operator +(MyCalc a, MyCalc b)
{
return new MyCalc(a.a * 3 ,b.b * 3);
}
}

In the preceding example, we can see that the plus sign (+) is overloaded with another kind of calculation. So if you sum up two MyCalc objects, you will get an overloaded result instead of the normal sum, and this overloading happens at compile time, so it is static polymorphism.

Dynamic polymorphism refers to the use of the abstract class. When you write an abstract class, no instance can be created from that abstract class. When any other class uses or implements that abstract class, the class also has to implement the abstract methods of that abstract class. As different classes can implement the abstract class and can have different implementations of abstract methods, polymorphic behavior is achieved. In this case, we have methods with the same name but different implementations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.41.235