Loading...

Difference Between Function Overloading and Function Overriding

Function Overloading:


Methods of the same name can be declared in the same class, as long as they have different sets of parameters (determined by the number, types and order of the parameters). This is called method overloading

Example:

   class Student
     {
        public void study() // study method with no parameter
        {

        }
        public void study(int a) // with one parameter
        {

        }
        public void study(int a, string b) // with two parameters
        {

        }
        public void study(string b) // with one parameters but of different data type
        {

        }
   }

Method Overriding:

Method Overriding is simply re definition of a Parent Class method/Function in Child Class method/Function. 


class Graphic
{
    public virtual void Draw()
    {//Function to draw object on screen
        Console.WriteLine("Graphic.Draw() ...");
    }
}

class Line : Graphic
{
    public override void Draw()  // new implementation of Draw method
    {//Specifically draws line object
        Console.WriteLine("Line.Draw() ...");
    }
}

class Point : Graphic
{
    public override void Draw()
    {//Specifically draws point object
        Console.WriteLine("Point.Draw() ...");
    }
}

class Rectangle : Graphic
{
    public override void Draw()
    {//Specifically draws rectangle object
        Console.WriteLine("Rectangle.Draw() ...");
    }

}

Difference Between Method Overloading and Method Overloading:

Function Overloading
Function Overriding
Possibility of Calling same function with different ways
Redefinition of Parent Scope function in child scope towards more specification
Same Scope
Different Scope
Same name but Difference in:
      No. of Arguments
      Type of any Argument
Exactly same Signature i.e.
      Same no. of Arguments
      Same return type
      Same types of Arguments
Independent of return type
Same return type


Hit Like Button if you understood, It helps a lot to keep us Motivated.

1 comments:

Post a Comment

Google+

 
TOP