In this article we will learn about one of the reusable object oriented features of C#, we will learn about polymorphism from the basics because I wrote this article focusing on students and beginners. Before proceeding further please refer to my previous articles for a better understandability.
Output
In the preceding program we declare the Virtual method that returns 10 and the same method we are using in the class Amount using the override keyword that at runtime returns 500 without changing the values of the method, even the names are the same, so the example above shows runtime polymorphism. In my next article, we will see in detail a program with runtime polymorphism.
Let's now begin learning about polymorphism with a definition.
What is Polymorphism?
Polymorphism means one thing having many (poly) forms. Suppose the example shown in the following diagram:In the preceding example a vehicle is something that has various forms; two-wheeler, three-wheeler and four-wheeler and so on. So how to differentiate each form in the preceding example using wheels (parameters)? Let us see more about polymorphism.
There are two types of polymorphism; they are:
Compile Time Polymorphism
Compile time polymorphism is done at compile time. The following are examples of compile time polymorphism.
- Method overloading
- Operator overloading
Method Overloading
Creating multiple methods in a class with the same name but different parameters and types is called method overloading.
Method overloading can be done in any of the following ways:
- By changing the number of parameters used.
- By changing the order of parameters.
- By using different data types for the parameters.
namespace BAL { public class Methodoveloading { public int add(int a, int b) //Method 1 { return a + b; } public int add(int a, int b, int c) //Method 2 { return a + b + c; } public float add(float a, float b, float c, float d) //Method 3 { return a + b + c + d; } } }
In the preceding method their are three methods with the same name but different parameter type. This is called method overloading. In my next article we will see the program method of overloading in details.
Run Time Polymorphism
Run time polymorphism happens at run time, in other words values are passed at runtime to the method. Runtime polymorphism can be done using method overriding.
Method Overriding
Creating the method in a derived class with the same name, same parameters and same return type as in the base class is called method overriding.
- Method overriding is only possible in a derived class, not within the same class where the method is declared.
- Only those methods overridden in the derived class declared in the base class using the virtual keyword or abstract keyword.
namespace BAL { public class methodoverriding { public virtual int balance() { return 10; } } public class Amount : methodoverriding { public override int balance() { return 500; } } }
10 and 500
In the preceding program we declare the Virtual method that returns 10 and the same method we are using in the class Amount using the override keyword that at runtime returns 500 without changing the values of the method, even the names are the same, so the example above shows runtime polymorphism. In my next article, we will see in detail a program with runtime polymorphism.
Difference between Method Overloading and Method Overriding
Method overloading
Creating multiple methods in a class with the same name but different parameters and types is called method overloading.
Method overriding
Creating the method in a derived class with the same name, the same parameters and the same return type as in a base class is called method overriding.
Difference between Virtual Method and Abstract Method
FAQ's
Can method overloading have the same number of parameters with different return types?
No, because a conflict occurs in methods when passing the parameters.
What is operator overloading?
We can redefine operators like +, - and * with additional functionality.
Summary
I hope you have learned an overview of polymorphism and its types. In the next article, we will learn each polymorphism type in detail. If you have any suggestions regarding this article, then please contact me.
2 comments
Nice explanation :)
Replythanks sir
ReplyPost a Comment