Tuesday, August 15, 2017

Method overriding

Overriding is can be defined when a class function invoked by an another inherited class function with the same name of the function.It usually uses reference of the base class. An example can be given if a parent class features of the function by default get by the child class.If the child class wants to change the feature of the parent class function there need overriding. For method overriding in c#, there need two keywords virtual and override.

Example:


  class Parents
    {
        public virtual void Greetings()
        {
            Console.WriteLine("80% of the wealth get by child");
        }
    }
    class Child : Parents
    {
        public override void Greetings()
        {
            Parents.Greetings();
            Console.WriteLine("100% of the wealth get by child");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Parents obj1 = new Child();
            obj1.Greetings();
            Console.ReadLine();
        }
    }

No comments:

Post a Comment