Showing posts with label Method OverRiding. Show all posts
Showing posts with label Method OverRiding. Show all posts

Saturday, 19 September 2015

Method Overloading And Method OverRiding

Method Overloading refers to Concept the defining the one or more functions or methods with same name with different Parameter Length or Parameter Types . So Functions are differenciated by their signature or Parameters .

Example For Method OverLoading

//Overloading
public class test
{
    public void getStuff(int id)
    {}
    public void getStuff(string name)
    {}
}
 
Method OverRiding is Totally Different Concept As Compared to Method Overloading . Method OverRiding Refers to Redefining the Function that Correspond to Some Other Class and Accessed Using Inheritance .

The Concept of Method OverRiding depends on Two Keywords - Virtual and OverRide

In Simple Scenario to Explain Method OverRiding with Example is Lets Assume We have a Base Class with function Sample() in Base Class then The Oher class that Derived From that Base Class Can Redefine the Functionality of That Sample() Function refers to Concept of Method OverRiding .

Example1  For Method OverRiding

class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();    

     b = new DC();
     b.Display();    
  }
}

Output

BC::Display
DC::Display
 

Example1  For Method OverRiding

//Overriding
public class test
{
        public virtual getStuff(int id)
        {
            //Get stuff default location
        }
}

public class test2 : test
{
        public override getStuff(int id)
        {
            //base.getStuff(id);
            //or - Get stuff new location
        }
}