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.

30 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. Replies
    1. nice work............ :)

      Delete
  9. Nicely explained.

    ReplyDelete
  10. Easy to understand Thank u soo much

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

    ReplyDelete
  12. Nice Article... Thanks

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

    ReplyDelete
  14. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in velachery | dot net training in chennai

    ReplyDelete

  15. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training in velachery

    ReplyDelete