blog

Home / DeveloperSection / Blogs / Method Overloading & Overriding

Method Overloading & Overriding

Anchal Kesharwani3917 13-Aug-2014

In this blog I’m explaining method overloading and method overriding.

In the Object oriented programming’s polymorphism is the special feature. Polymorphism makes from Greek two words polys, “many, much” and morphe, “form, shape” that means polymorphism is the many forms. Polymorphism have one name many tasks do.

There are two kinds of polymorphism:

·    Method Overloading

·    Method Overriding

Method Overloading

Method overloading is also known as compile time polymorphism. Method overloading occurs when two or more methods in the same class or sub class have the exact same name but different parameters (remember that method parameters accept values passed into the method). Here give a suitable following example for try to best understand method overloading.

Example

using System;
namespace MethodOverloading
{
    classCalculator
    {
        publicint Sum(int a, int b)     // create sum method which take two integer value
        {
            return a + b;    // return sum of two integer
        }
        publicfloat Sum(float a, float b)   // overload sum method with return type change and argument also change
        {
            return a + b;    // return sum of two float
        }
    }
    classSubClassOfOverloading : Calculator
    {
        publicfloat Sum(int a, int b, float c) // overload sum method with return type change and argument also change into subclass
        {
            return a + b + c;   // return float sum of two integer and one float value
        }
    }
    classTest
    {
        staticvoid Main(string[] args)
        {
            SubClassOfOverloading subClassObj = newSubClassOfOverloading(); // create an instance of subclass
            Console.WriteLine(subClassObj.Sum(15, 20));          // call the sum method and Output: 25  
            Console.WriteLine(subClassObj.Sum(20.20f, 25.70f));  // call the sum method and Output: 45.9
            Console.WriteLine(subClassObj.Sum(10, 20, 30.50f));  // call the sum method and Output: 60.5
            Console.Read();
        }
    }
}

Method Overriding

Method overriding is also known as run time polymorphism. Creating a method in derived class with same signature as a method in base class is called as method overriding. Same signature means the method have same name, same return type and same parameter. Method overriding is not possible in one class it must have two or more class. Method overriding is possible only in derived classes. To allow the derived class to override a method of the base class, C# provides two options, virtual methods and abstract methods. Virtual keyword property must override that method with override keyword into derived class.

Example 1

using System;
namespace MethodOverride
{
    classBaseClass
    {
        publicvirtualstring Hello()       //create a method with virtual keyword that return string
        {
            return"First time method create into Base class.";
        }
    }
    classDerivedClass : BaseClass  //inherit the BaseClass into DerivedClass
    {
        publicoverridestring Hello()  // Derived class must override Hello method
        {
            return"Override method create into Derived class.";
        }
    }
    classTest
    {
        staticvoid Main(string[] args)
        {
            DerivedClass obj = newDerivedClass();  // create an instance of DerivedClass
            string message = obj.Hello();     // call the Hello method
            Console.WriteLine(message);   // override message Output is: Override method create into Derived class.
            Console.Read();
        }
    }
}

Example 2

using System;
namespace MethodOverrideExample
{
    abstractclassBaseClass
    {
        publicabstractstring Hello(); //declare a method with abstract into abstract class keyword that return string
    }
    classDerivedClass : BaseClass  //inherit the BaseClass into DerivedClass
    {
        publicoverridestring Hello() // Derived class must override Hello method
        {
            return"Override method create into Derived class.";
        }
        publicint sum(int x, int y) 
        {
            return x + y;
        }
    }
    classTest
    {
        staticvoid Main(string[] args)
        {
            DerivedClass obj = newDerivedClass();   //create an instance of DerivedClass
            Console.WriteLine(obj.Hello());         // Call Override method Output is: Override method create into Derived class.
            Console.WriteLine(obj.sum(10, 20));     // Output :30
            Console.ReadKey();
        }
    }
}


Updated 18-Sep-2014

Leave Comment

Comments

Liked By