In previous section, we have seen Inheritance and one kind of Polymorphism that is Overriding. In this section, we will delve further and see how Method Overloading works.
Method Overloading follows a set of rules. They are:-
- Different Number of Parameters
- Different Types of Parameters
One more important thing to understand here, signature of method means its name and parameter list not the return type.
Also, it’s important to understand difference between Overloading and Overriding.
- Overloading means same method name with different parameters or types of parameters in the same class.
- Overriding means virtual methods gets overridden in derived class.
Now, let’s look at the demo for the same. Below, I have added Overloaded method.
Now, let me go ahead and call the same from the main program as shown below.
With the above change in place, When I run the program, then it will produce the following output.
Since, this method is virtual, hence I can override this as well in the derived class as shown below.
Then, in the main class.
Which means, here method got overloaded and also overridden.
I hope you would have enjoyed this session. We’ll delve further in coming section. Till then, stay tuned and Happy coding.
Download Link:- https://github.com/rahulsahay19/csharp-Basics
2,994 total views, 1 views today