articles

Home / DeveloperSection / Articles / Method Overriding in C# Net

Method Overriding in C# Net

Uttam Misra6534 16-Jul-2010

Overriding a method is to change the behavior of the method for the derived class.

Overriding is done using inheritance and virtual functions. 

Example

To explain the concept of overriding I have created two classes Poly

and abc(inherits poly).

    public class poly
    {
//these two functions, sum(), are overloaded.
        public int sum(int x, int y)
        {
            return (x + y);
        }
        public virtual int sum(int x, int y, int z)
        {
            return (x + y + z);
        }
    }
 
    public class abc : poly
    {
//overriding function sum(), of base class poly
        public override int sum(int x, int y, int z)
        {
            return (x + y + z + 10);
        }
    }

 

 


Updated 04-Mar-2020
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By