Background
Mostly people are confused in object oriented concept that is Overloading and Overriding and interviewer take a benefit and make it complicate for them. Here, I will start for Overloading and Overriding from its root level as every beginner or expert can under stand this concept.
What is Method Overloading?
Class can have multiple method with same name and different signature and type is called Method overloading. It is example of compile time polymorphism which is done at compile time. This is the basic definition about method overloading but now we'll how we can achieve it in our programming, there are several ways to achieves which are listed as below.
Steps to achieve Method overloading.
Mostly people are confused in object oriented concept that is Overloading and Overriding and interviewer take a benefit and make it complicate for them. Here, I will start for Overloading and Overriding from its root level as every beginner or expert can under stand this concept.
What is Method Overloading?
Class can have multiple method with same name and different signature and type is called Method overloading. It is example of compile time polymorphism which is done at compile time. This is the basic definition about method overloading but now we'll how we can achieve it in our programming, there are several ways to achieves which are listed as below.
Steps to achieve Method overloading.
- By changing the number of parameter in method signature.
- By change parameter datatype in signature
- By changing parameter order in signature
So, as display in above image we can create multiple method with same name but different signature and data type this called as method Overloading or you can call it as a Compile time polymorphism as well.
Here, before moving to our next topic I like to discuss one thing that is sometimes in interview interview is asking question like
Question - if in class ther are two method with same name and signature but they having different return type, so is it call as method Overloading or not ?
Answer for this question is No, its notmethod overloading as both method having same parameter, so it will be conflict in between two methods.
In overloading there is one more concept is there, it is Method Hiding concept.
In base and child class both having method with same name and same signature at that moment if you are running application so your application will run successfully but in Warning section you will find a warning that is, suggesting you to use new keyword before child class method. Please refer below image.
In above image you can clearly see that after running this project in warning section it gives warning for New keyword. So, for method hiding we are using new keyword in overloading concept.
Now, we will move to our next topic that is Method Overriding in C#.
Method Overriding
Based class and child class having method with same name, same parameter and same return type is called method overriding. It is example of runtime polymorphism.
Method overriding is only possible with child class where class having parent child relationship and where child class method is overriding base class method. So, in same method cannot do overriding.
For method overriding we are using two key words, Virtual and Override.
In base class we are using virtual keyword and in child class override keyword that means child class method is overrides parent class method.
So, as display in image base and child class having same method with same signature and return type but we have override parent class method by adding Override keyword to child class method and adding Virtual keyword to base class method.
I hope you enjoy learning of Overloading and Overriding concept through this examples and detail information of both concept.
Summary
I hope this article is useful for all beginners and programmers. If you have any suggestion related to this article then please give your comment, I'll give your comment answer.
No comments:
Post a Comment