Home > DeveloperSection > Forums > How to use override concept using virtual keyword in C#
Ankit Singh

Total Post:341

Points:2389
Posted on    January-03-2016 10:59 PM

 C# C#  OOPS 
Ratings:


 1 Reply(s)
 245  View(s)
Rate this:
I want to use use  override  concept using  virtual  keyword in C#.how to do this please help me.


aditya kumar Patel

Total Post:244

Points:1736
Posted on    January-03-2016 11:08 PM

 we know overriding is the concepts of OOPS which is implement using abstract method and virtual class keyword . for more description goto  https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx .
 Let's see on virtual and override keywords in C# .

try to how can do show below

using System;

namespace Verriding
{
    class first
    {
        public virtual void method() { Console.WriteLine("called class first method()"); }
    }

    class second : first
    {
        public override void method() { Console.WriteLine("called class second method()"); }
    }

    class third : second
    {
        public override void method() { Console.WriteLine("called class third method()"); }
    }

    class Program
    {
        static void Main(string[] args)
        {

            first a = new first();
            second b = new second();
            third c = new third();
            a.method(); // output --> "first::method()"
            b.method(); // output --> "second::method()"
            c.method(); // output --> "third::method()"
          
            a = new second();
            a.method(); // output --> "second::method()"
            b = new third();
            b.method(); // output --> "third::method()"

            Console.ReadKey();
        }
    }
}


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

Follow MindStick