Jitesh Byahut 10/9/2014 2292

Override In C#

Override is use to modify any virtual or abstract method, property, indexer or event. Override method provides a new implementation of member in derived class. In overriding concept, both base class and derived override member have same signature. You can only override member which is type of virtual, abstract and override. You cannot use new, static or virtual keywords with override method.

Override a virtual method

Here we are defining a virtual method in base class and overriding that method in derived class. Both method have same signature.

class TestClass

{

    public virtual void Multiply(int a, int b)

    {

        Console.WriteLine("Multiplication performed here");

    }

}

 

class Program : TestClass

{

    // override virtual method

    public override void Multiply(int x, int y)

    {

        Console.WriteLine(x * y);

    }

 

    static void Main(string[] args)

    {

        Program obj = new Program();

        obj.Multiply(2, 8);

        Console.Read();

        // Output:- 16

    }                

}

Override an abstract method

Here we are defining an abstract method inside an abstract class and that method is overriding in derived class. In abstract class, abstract method has only declaration and we define implementation of that method in derived class.

abstract class MathClass

{

    public abstract int Multiply(int a, int b);

}

 

class Program : MathClass

{

    // override abstract method

    public override int Multiply(int x, int y)

    {

        return x * y;

    }

 

    static void Main(string[] args)

    {

        Program obj = new Program();

        Console.WriteLine(obj.Multiply(2, 8));

        Console.Read();

        // Output:- 16

    }               

}

Override an overridden method

Here we are showing that you can override a method that already overridden a method. First we are defining a virtual method inside VirtualClass that override in OverrideClass class. Again after that this overridden method override in Program class. If you override a method then at run time, most derived method is called.

class VirtualClass

{

    public virtual void DisplayMessage()

    {

        Console.WriteLine("virtual method");

    }

}

class OverrideClass : VirtualClass

{

    // override virtual method

    public override void DisplayMessage()

    {

        Console.WriteLine("override a virtual method");

    }

}

 

class Program : OverrideClass

{

    // override overriden method

    public override void DisplayMessage()

    {

        Console.WriteLine("override a overridden method");

    }

 

    static void Main(string[] args)

    {

        Program obj = new Program();

        obj.DisplayMessage();

        Console.Read();

        // Output:- override a overridden method

    }               

}

Override a property

Here we are overriding a property. We are defining a virtual property MovieRating of type integer inside base class and that override in derived class. We provide logics in override implementation.

Property MovieRating provides user to give their ratting on movie. Here max movie ratting is 5, but suppose any user give more than 5 rating, then by default its showing 5 star rating.

class TestClass

{

    public virtual int MovieRating { get; set; }

}

   

 

class Program : TestClass

{

    // override property

    public override int MovieRating

    {

        get

        {

            return base.MovieRating;

        }

        set

        {

            if (value >5 )

            {

                value = 5;

            }

            base.MovieRating = value;

        }

    }

 

    // You cannot use here static

    public void ShowRating()

    {

        Console.WriteLine("You movie ratting is " + MovieRating);

    }

 

    static void Main(string[] args)

    {

        Program obj = new Program();

        obj.MovieRating = 6;

        obj.ShowRating();

        Console.Read();

        // Output:- You movie ratting is 5

    }               

}

Override ToString()

Here we are overriding ToString().

class Program

{

    public string Name { get; set; }

 

    // override ToString()

    public override string ToString()

    {

        return Name;

    }

 

    static void Main(string[] args)

    {

        Program obj = new Program();

        obj.Name = "asphelps";

        Console.WriteLine(obj.ToString());

        Console.Read();

        // Output: asphelps

    }

}



Please give your feedback for improving this page