Differentiate between overloading and overriding in C#.

 


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 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 have different access specifier. Method should be public.
class abc
{
 public void Disp(int a)
 {  
  Console.WriteLine(a);
 }
 public void Disp(int a, int b)
 {  
  Console.WriteLine("{0}{1}",a,b);
 }
}
class Program
{
 static void Main(string[] args)
  {
   abc a1 = new abc();
   a1.Disp(10)
   Disp(10, 20);
  }
}
public class MyBaseClass
{
 public virtual void print1()
 {  
  Console.WriteLine("Base imp");
 }
}
public class MyDerivedClass:
MyBaseClass
{
 public override void print1()
 {  
  Console.WriteLine("Derived imp");
 }
}
class Program
{
 static void Main(string[] args)
 {
   MyDerivedClass myobj = new MyDerivedClass();
   MyBaseClass myBaseObj = myobj;
   myBaseObj.print1();
   Console.ReadKey();
 }
}