Difference between Function Overriding and Function Overloading
Overriding is the example of run-time polymorphism and
Overloading is the example of compile-time polymorphism.
The Compile-Time polymorphism have early binding and
Runtime polymorphism have late binding.
The Compile-Time polymorphism is Static and
Runtime polymorphism is Dynamic.
Overriding
¦The return type must exactly match that of the overridden method (Note:- in some languages we can change Return type in overriding).
¦The access level must not be more restrictive than that of the overridden method.
¦The access level can be less restrictive than that of the overridden method.
¦The overriding method must not throw new or broader checked exceptions than those declared by the overridden method.
¦The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
¦You cannot override a method marked final.
¦Overriding is only possible through inheritance.
¦If a method can’t be inherited, you cannot override it.
Overloaded method
¦Overloaded methods must change the argument list (you can do change in argument list by 1. changing datatype of parameters, 2. changing sequence of parameters, 3. changing count of parameters).
¦Overloaded methods can change the return type (mins its doesn't make any difference that either you change return type or not. its not matter for overloading)
¦Overloaded methods can change the access modifier.
¦Overloaded methods can declare new or broader checked exceptions.
¦A method can be overloaded in the same class or in a subclass.
NOTE:- all most all object oriented languages supports function overloading and function overriding and some of languages also supports operator overloading like : c#.net , c++ etc and some languages not support operator overloading like: vb.net but no language support operator overriding.
Difference between Function Overriding and Function Overloading
Posted by
Rajesh Rolen
at
Monday, September 14, 2009
Labels: ASP.NET , ASP.NET Interview Questions , C#.NET and VB.NET Interview Questions , Interview Questions
0 comments:
Post a Comment