- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 475字
- 2021-07-02 12:44:42
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.
- 高手是如何做產(chǎn)品設(shè)計的(全2冊)
- Modular Programming with Python
- Docker and Kubernetes for Java Developers
- Python for Secret Agents:Volume II
- Scratch 3.0少兒編程與邏輯思維訓(xùn)練
- Learning AndEngine
- 單片機應(yīng)用與調(diào)試項目教程(C語言版)
- Babylon.js Essentials
- Android應(yīng)用開發(fā)深入學(xué)習(xí)實錄
- Visual Basic程序設(shè)計(第三版)
- Machine Learning for Developers
- Python預(yù)測分析與機器學(xué)習(xí)
- Responsive Web Design with jQuery
- 體驗之道:從需求到實踐的用戶體驗實戰(zhàn)
- Mastering ASP.NET Web API