↑ Grab this Headline Animator

Tuesday, April 21, 2009

Overloading and Overriding - Method Vs Author

A method overriden has exactly the same skeleton as the method in the parent class. Same Access modifier, same type of return value, same name and same arguments. Only thing that can be changed is the inner content of the method.

Method: OK Boss !! I won't mind if you change my main code , your wish ...
Author: Hey method , don't take it to heart,we just do it only when we inhert and override a method, only when its required.

Method: How about the option of having so many Constructors and the Constructor is chosen based on the arguments passed.

Can't method have that option.I too want to provide the option of giving different methods and the user can chose the method to use based on the arguments passed.

Author: Yes, we can do that. That is called OVERLOADING.

Method: Overloading ... hmm..

Author : You can have many methods with the same name but with different arguments.

Method: cool ...

Author : yes it is .. While Overloading the most important rule is you should have the same method name.

Method: Yes, its obvious. Similar to the constructor.

Author: There are more, You cannot just change the return type. You should change the argument list which is to be passed. If you don't change the argument list, it becomes the overriding. You have to give different arguments. You can give different return types, it is not compulsory , you can either change it or retain the same return type.

Method: Too many rules all the time. Not fair..

Author: yes. Life is not fair, get used to it.

Method: Don't copy Bill Gate's quote.

Author: hehe ..I am just reminding you.

We don't have too many rules here.
A method has the following parts which can be changed.

<access modfiier> <return type> <methodname> ( <argument list> ) {
// method content


If we change the method name, its all together new method ,so no headaches.

If we change the argument list keeping all others the same( except the content , it should be changed all the time), it is method overriding. Overriding can be done only in Inheritance.

If we try to change any others, like return type , argument list it is called method overloading. It can be done in any class, need not be Inheritance or others.Access modifiers can be changed while overloading.

Method: ok ,convinced.

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles