Monday, September 14, 2009

Difference between Function Overriding and Function Overloading

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.

¦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++ etc and some languages not support operator overloading like: but no language support operator overriding.

Share This!

1 comment:

Anonymous said...

Hi, i read your blog from time to time and i own a similar one
and i was just curious if you get a lot of spam feedback?
If so how do you protect against it, any plugin or anything you
can advise? I get so much lately it's driving me mad so any support is very much appreciated.

Also visit my page my training video producers help

Powered By Blogger · Designed By Seo Blogger Templates