Sunday, August 17, 2014

Understanding Runtime Polymorphism

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApps
{
    class BaseClass
    {
        public void Normal()
        {
            Console.WriteLine("Base Class print");
        }
        public virtual void RuntimePoly()
        {
            Console.WriteLine("Base Class print");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void Normal()
        {
            Console.WriteLine("Derived Class print");
        }

        public override void RuntimePoly()
        {
            Console.WriteLine("Derived Class print");
        }
    }

class UnderstandingRuntimePolymorphism
{
 public static void Main()
{
           // Here Base class method is called
           BaseClass nm = new DerivedClass();
           nm.Normal();

          // Here Derived class method is called
            // Due to run time decision (late binding or runtime polymorphism)
            // since the methods are overriden with ovveride keyword in derived class
            // and virtual keyword in base class of the methods

           BaseClass poly = new DerivedClass();
           poly.RuntimePoly();
     }
 }
}


No comments:

Post a Comment