What polymorphism is
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.
For example:
- 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;
- }
- }
- }
Run time polymorphism
Run time polymorphism happens at run time, in other words values are ed at runtime to the method. Runtime polymorphism can be done using method overriding.
Output
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 overriden in the derived class declared in the base class using the virtual keyword or abstract keyword.
For example:
- 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 details 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
Some of the FAQs on polymorphism are:
Question: Can method overloading have the same number of parameters with different return types?
Ans: No, because a conflict occurs in methods when ing the parameters.
Question: What is operator overloading?
Ans: We can redefine operators like +, - and * with additional functionalities.
No comments:
Post a Comment