Sunday, November 4, 2012

Difference Between Function Overloading and Function Overriding

Function Overloading and Function Overriding are also known as Static and Dynamic Polymorphism

  • Method overriding means having a different implementation of the same method in the inherited class.
  • Method overloading means having two or more methods with the same name but different signatures.

  • Overridden methods would have the same signature, but different implementation. 
  • Overloaded methods have different signature but the same name.

  • One of the overridden methods would exist in the base class and another in the derived class. These cannot exist in the same class.
  • Overloaded methods can exist in base class and derived class or in the same class.

  • Overriding is resolved at runtime.(Dynamic Polymorphism)
  • Overloading is resolved at compile time.(Static Polymorphism)

  • You can not override static, final and private methods
  • You can overload static, final or private methods

  • An overloaded method can throw any exception thrown / not thrown by the method it overloads. 
  • An overriding method can only throw those checked Exceptions and subclasses of these Exceptions, that are thrown by the method it overrides. The overriding method can throw any Runtime Exception.

  • Access Specifier of Overriding method can be only greater than or equal to the access specifier of the overridden method.
  • Overloading methods can have any access specifiers
Here is an excellent example - http://stackoverflow.com/a/154939/602683

No comments:

Post a Comment