Home > DeveloperSection > Blogs > Method Overloading & Overriding

Method Overloading & Overriding


C# C# 
Ratings:
0 Comment(s)
 2056  View(s)
Rate this:

Method Overloading & Overriding

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

{

    class Calculator

    {

        public int Sum(int a, int b)     // create sum method which take two integer value

        {

            return a + b;    // return sum of two integer

        }

        public float Sum(float a, float b)   // overload sum method with return type change and argument also change

        {

            return a + b;    // return sum of two float

        }

    }

    class SubClassOfOverloading : Calculator

    {

        public float 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

        }

    }

    class Test

    {

        static void Main(string[] args)

        {

            SubClassOfOverloading subClassObj = new SubClassOfOverloading(); // 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

{

    class BaseClass

    {

        public virtual string Hello()       //create a method with virtual keyword that return string

        {

            return "First time method create into Base class.";

        }

    }

    class DerivedClass : BaseClass          //inherit the BaseClass into DerivedClass

    {

        public override string Hello()      // Derived class must override Hello method

        {

            return "Override method create into Derived class.";

        }

    }

    class Test

    {

        static void Main(string[] args)

        {

            DerivedClass obj = new DerivedClass();  // 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

{

    abstract class BaseClass

    {

        public abstract string Hello();       //declare a method with abstract into abstract class keyword that return string

    }

    class DerivedClass : BaseClass          //inherit the BaseClass into DerivedClass

    {

        public override string Hello()      // Derived class must override Hello method

        {

            return "Override method create into Derived class.";

        }

        public int sum(int x, int y) 

        {

            return x + y;

        }

    }

    class Test

    {

        static void Main(string[] args)

        {

            DerivedClass obj = new DerivedClass();   //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();

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick