Polymorphism
- It is an approach in behaving in different way depending upon the received input that is whenever the input change then automatically the output or behaviors will change.
- We can implement polymorphism into our application by using three different approaches
- Overloading
- Overriding
- Hiding/Shadowing
Overloading : - This is again three types
- Method Overloading
- Constructor Overloading
- Operator Overloading
Method Overloading : It’s an approach of defining multiple method under a class with the same name changing the parameter of that method that is we can change either the number of parameters passed to the method or type of parameter passed to the method or order of parameter passed to the method.
Example :
Note : A change in the return type of method will not be taken into consideration in overloading.
Add a class "Loadmethods" and write the following code:
In overloading behavior or output of methods will change based on the parameter value of that method that is type of value, number of value as well as order of values.
Method overloading is an approach of defining multiple behaviors to a method and that method start behaving in different ways based on the parameter of that method.
Example
In the above case the IndexOf () method of string class will return the index of a particular character under the string but the difference is the first method return the first occurrence of the character and the second method returns the next occurrence of the character so in this case the name of method is same. The only difference is the parameter of the method.
The WriteLine () method of the console class is also a good example for overloading because the method has 19 different overloads can print any value that passed as parameter to the method.
Example :
Add a class "Loadmethods" and write the following code:
In overloading behavior or output of methods will change based on the parameter value of that method that is type of value, number of value as well as order of values.
Method overloading is an approach of defining multiple behaviors to a method and that method start behaving in different ways based on the parameter of that method.
Example
The WriteLine () method of the console class is also a good example for overloading because the method has 19 different overloads can print any value that passed as parameter to the method.
No comments:
Post a Comment