In C#, if a class has multiple methods of the same name but their parameters are different then it is called method overloading. In other words, 'method overloading is a feature of C# in which a class can have more than one method with the same name and their parameters are different.'
This is similar to constructor overloading.
Advantage
1. Method overloading increases the readability of the program.
2. It provides flexibility to programmers so that they can call the same method for different types of data.
3. This makes the code look clean.
4. It reduces the execution time because the binding is done in the compilation time itself.
5. With this we can use the code again, which saves memory.
6. Method overloading reduces the complexity of the code.
Syntax
[datatype] method-name(datatype arguments){
Statement;
}
[datatype] method-name(datatype arguments, datatype arguments){
Statement;
}
Method Overriding
If the subclass (child class) has the same method which is declared in the parent class then it is called method overriding in java. In other words, 'declaring a method in a subclass that already exists in the parent class is called method overriding'.
If a method in a subclass has the same name, same parameters, and same return type as the method of the super-class, then the method of the subclass is said to be an overriding method.
Rules of method overriding
1. The method name should be the same for both child class and parent class.
2. Both parent-class and child-class should have the same (same) return type and same parameter list.
3. We cannot override the method declared as final and static.
4. The access modifiers of the child class method should not be more restrictive than the method of the parent class. For example – If we have declared the parent-class method as public then we cannot declare child-class as private and protected.
5. Instance methods can be overridden only if they are inherited by the subclass.
6. If a method cannot be inherited then it cannot be overridden either.
7. We should always override the abstract methods of the parent class.
8. Constructors cannot be overridden.
Syntax [datatype] method-name(datatype arguments){ Statement-1; } [datatype] method-name(datatype arguments){ Statement-2; }
Differentiate between Method Overloading and Method Overriding
Overloading | Overriding |
its define in the same class | its define in child class |
its depend on a parameter but function name are same | function name and arguments are the same but the internal statement is change |
It applies to compile time | It applies to the run time |
Example
using System;
class Demo {
public void PrintPattern(int length){
for(int i=1; i<=length;i++)
Console.Write('{0}{1}',i,i < length ? ',':'');
Console.WriteLine();
}
// overload the PrintPattern method 2 parameter
public void PrintPattern(int length, char ch){
for(int i=1; i<=length;i++)
Console.Write('{0}{1}',ch,i < length ? ',':'');
Console.WriteLine();
}
// overload the PrintPattern method with 3 parameter
public void PrintPattern(int length, char ch, string seperator){
for(int i=1; i<=length;i++)
Console.Write('{0}{1}',ch,i < length ? seperator:'');
Console.WriteLine();
}
// making SayHello function for override
public virtual void SayHello(){
Console.WriteLine('Say Hello in Parent Class');
}
}
class Demo2 : Demo
{
// override sayhello method from parent class
public override void SayHello(){
Console.WriteLine('Say Hello in child Class');
}
}
public class Program
{
public static void Main()
{
Demo dm=new Demo();
dm.PrintPattern(10);
dm.PrintPattern(10,'*');
dm.PrintPattern(10,'*','!');
dm.SayHello();
Demo dm2=new Demo2();
dm2.SayHello();
}
}
Output
1,2,3,4,5,6,7,8,9,10
*,*,*,*,*,*,*,*,*,*
*!*!*!*!*!*!*!*!*!*
Say Hello in Parent Class
Say Hello in child Class