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
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. |
Very good exaplanation!
ReplyDeleteVery good exaplanation!
ReplyDeletegood work man
ReplyDelete"Overriding" Method should be public/protected(inherited)/internal(if same assembly).
ReplyDeleteCheck this one....Difference between method overloading and method overriding
DeleteLing
good..
ReplyDeleteEasily understood to this explanation
Conceptual and perfectly explained
ReplyDeleteVery Good Work!
ReplyDeletegood explanation
ReplyDeleteThank u
good one
ReplyDeleteVery Nice
ReplyDeletevery nice
Deletenice work............ :)
DeleteExcellent Thank u
ReplyDeletegood thnks
ReplyDeleteNicely explained.
ReplyDeletethank u
ReplyDeletegood one
ReplyDeletevery good!
ReplyDeleteGood Work..!
ReplyDeleteEasy to understand Thank u soo much
ReplyDeleteVery use full...Easy to understand Thank u soo much
ReplyDeleteNice Article... Thanks
ReplyDeleteit was much usefull for our 7th sem ISE internal assessment test.. THANK U bro NMT..!!
ReplyDeleteExcellent explanation Difference Between Method Overloading and Method Overriding
ReplyDeleteGreat post about Java Difference between method overloading and overriding
ReplyDeletethanks
ReplyDeleteThanks 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
In overloading, methods can not have different return type. In your example, square() has int, long, double return types which is wrong.
ReplyDelete