Home > DeveloperSection > Blogs > Method Overriding in C#

Method Overriding in C#


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

Method Overriding in C#

In this blog, I’m trying to explain the concept of method overriding.

Method overriding is the concept of polymorphism and also uses the inheritance. In method overriding, firstly we inherit the base class method into derived class and then changes its functionality according to our need.

When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.

Method Overriding requires:

1-Same method name in both base class and derived class

2-Number of parameters should be same.

3-Data type of parameters should be same.

Method overriding is possible only in derived classes, but not within the same class. When derived class needs a method with same signature as in base class, but wants to execute different code than provided by base class then method overriding will be used.

To implement the method overriding:

1-virtual or abstract keyword is used with method in base class.

2-override keyword is used with method in derived class.

Example 1

Using virtual and override keyword

using System;

 

namespace MethodOverridingConsoleApplication

{

    class BaseClass

    {

        /// <summary>

        /// cityname() with virtual keyword

        /// </summary>

        /// <returns></returns>

        public virtual string cityname()

        {

            return "New York";

        }

    }

    class DerivedClass : BaseClass//inherit the base class

    {

        /// <summary>

        /// cityname() with override keyword

        /// </summary>

        /// <returns></returns>

        public override string cityname()

        {

            return "London";

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            BaseClass bc = new BaseClass();

            DerivedClass dc = new DerivedClass();

            //calls the method

            Console.WriteLine("Your city :"+bc.cityname());

            Console.WriteLine("Your city :" + dc.cityname());

        }

    }

}

Output :

Your City : New York

Your City : London

In this example , i have created a method cityname() in base class and overrides it in derived class. In class Program , objects of base class bc and derived class dc are created and calls the same method one by one ,see the difference in output - when cityname() called with base class object bc outputs comes "New York" and when cityname() called with derived class object dc the output comes "London" ,it means that the functionality of cityname() method only changed for derived class, not for base class.

Example – 2

Using abstract and override keyword

using System;

 

namespace MethodOverridingConsoleApplication2

    {

        /// <summary>

        /// cityname() with abstract keyword in abstract class

        /// </summary>

        abstract class BaseClass

        {

            public abstract string cityname();

        }

        class DerivedClass : BaseClass

        {

            /// <summary>

            /// cityname() with override keyword

            /// </summary>

            /// <returns></returns>

            public override string cityname()

            {

                return "London";

            }

        }

        class Program

        {

            static void Main(string[] args)

            {

                DerivedClass dc = new DerivedClass();

                Console.WriteLine("Your city :" + dc.cityname());

            }

        }

    }

}

Output :

Your City : London

In this example, I have used abstract and override keyword , firstly you have create a abstract class to have a abstract method in your program then you can override it in your derived class , note that its mandatory to overrides all abstract method of the base class. 


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

Follow MindStick