Hiding base class method in C#

If the inherited member is virtual, we can override this implementation with the override keyword.

We can follow this code:


// Program: Example of Inheritence and overridding
// By: H.M. Saiful Islam

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

namespace OOP
{

    // Base Class
    public class BaseClass
    {
        public virtual void MyMethod()
        {
            Console.WriteLine("It's Base class's method");
        }
    }

    // Derived class
    public class DerivedClass : BaseClass
    {
        public override void MyMethod()
        {
            Console.WriteLine("Overridding example");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BaseClass MBC;
            DerivedClass MDB = new DerivedClass();
            MBC = MDB;
            MBC.MyMethod();
            Console.ReadKey();
        }
    }
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s