Sunday 23 September 2012

Difference between method overloading and method overriding

Method Overloading and Method Overriding are both the techniques used to implement FUNCTIONAL POLYMORPHISM 

Method Overloading (COMPILE TIME Polymorphism)
Method Overloading means having two or more methods with the same name but with different signature(different parameters list and different type of parameters) in same class or in different classes.

Method Overloading forms compile-time polymorphism.

Example :
We will create a method that will calculate the square of a given number having same name but different data type.In this program we have use different methods having same name but different parameters this is called overloading.

 

 

Method overriding (RUN TIME Polymorphism)
Method overriding means having two methods with same name and same signature, one method in base class and other method in derived class.

A subclass inherits methods from a base class. Sometimes, it is necessary for the subclass to modify the methods defined in the base class. This is referred to as method overriding.

This can be achieved by using the virtual and override keywords. we have to use the virtual keyword for the method which in base class and override keyword  for the method in subclass.

By default functions are not virtual in C# and so you need to write “virtual” explicitly.

Example :


    




























Difference between method overloading and method overriding

Overloading Overriding
Having same method name with different Signatures. Methods name and signatures must be same.
Overloading is the concept of compile time polymorphism Overriding is the concept of runtime polymorphism
Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading When a function of base class is re-defined in the derived class called as Overriding
It doesn't need inheritance. It needs inheritance.
Method can have different data types Method should have same data type.
Method can be different access specifies Method should be public.

28 comments:

  1. Very good exaplanation!

    ReplyDelete
  2. Very good exaplanation!

    ReplyDelete
  3. "Overriding" Method should be public/protected(inherited)/internal(if same assembly).

    ReplyDelete
  4. good..
    Easily understood to this explanation

    ReplyDelete
  5. Conceptual and perfectly explained

    ReplyDelete
  6. Very Good Work!

    ReplyDelete
  7. good explanation
    Thank u

    ReplyDelete
  8. Nicely explained.

    ReplyDelete
  9. Easy to understand Thank u soo much

    ReplyDelete
  10. Very use full...Easy to understand Thank u soo much

    ReplyDelete
  11. Nice Article... Thanks

    ReplyDelete
  12. it was much usefull for our 7th sem ISE internal assessment test.. THANK U bro NMT..!!

    ReplyDelete

  13. Thanks for sharing the knowledgeable stuff to enlighten us no words for this amazing blog.. learnt so many things I recommend everyone to learn something from this blogger and blog.. I am sharing it with others also
    IT Software Training in Chennai | Python Training in Chennai | Dot Net Training in Chennai |Android Training in Chennai | J2ee Training in Chennai

    ReplyDelete
  14. In overloading, methods can not have different return type. In your example, square() has int, long, double return types which is wrong.

    ReplyDelete