C# Method Overriding by Virtual methods | Virtual Keyword

Program


using System;

namespace Polymorphism
{
    class MethodOverriding
    {
        public class Person
        {
            public virtual void Print()
            {
                Console.WriteLine("I am Person Method");
            }
        }

        class Employee: Person
        {
            public override void Print()
            {
                Console.WriteLine("I am Employee method");
            }
        }

        static void Main(string[] args)
        {
            Person p;

            p = new Person();
            p.Print();  //I am Person Method

            p = new Employee();
            p.Print();  //I am Employee method

            Employee e = new Employee();
            e.Print();  //I am Employee method

            Console.ReadKey();
        }
    }
}

Output

I am Person Method

I am Employee method
I am Employee method




No comments:

Post a Comment