Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Sunday, February 13, 2011

# Difference between method overriding and overloading

Overriding is the concept of having functions of same name and signature in different classes. one in the super class can be made virtual and other can override the functionality of virtual one.

Overloading is the concept of having functions of same name, but different signature in same class. They are differentiated by the compiler by their signatures.


Overloading methods
1.They appear in the same class or a subclass
2.They have the same name but, have different parameter lists, and can have different return types.
An example of an overloaded method is print() in the java.io.PrintStream class

Overriding methods
It allows a subclass to re-define a method it inherits from it's superclass
overriding methods:
1. It appears in subclasses
2. They have the same name as a superclass method
3. They have the same parameter list as a superclass method
4. They have the same return type as as a superclass method
5. They have the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
·If the superclass method is public, the overriding method must be public
·If the superclass method is protected, the overriding method may be protected or public
·If the superclass method is package, the overriding method may be packagage, protected, or public
·If the superclass methods is private, it is not inherited and overriding is not an issue

--
Using overloading and overridding, you can acheive the concept of polymorphism.

Polymorphism means "one name, multiple forms". Using one name u can do multiple of actions...

Method overloading is a compile time polymorphism and Method Overridding is a runtime polymorphism...

Compile time polymorphism means compiler knows which object assigned to which class at the compiling time....Runtime polymorphism means compiler didn't know at the compile time, it only knows at a run time...

♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://www.geekinterview.com/talk/504-difference-between-method-overriding-and-overloading.html

1 comment: