Method overriding in C# – part 3

Let’s analyze just one more example of method overriding. In this case we have got three classes. TopClass is derived from IntermediateClass. IntermediateClass is derived from BaseClass. What if we are in the TopClass level and we want to perform some operations at the lower level and then go back, to the top level?

Well, look at the SomeJob method:

C# code

 class Program
    {
        static void Main(string[] args)
        {
            BaseClass bc = new BaseClass();
            bc.InvokeMethod();

            IntermediateClass ic = new IntermediateClass();
            ic.InvokeMethod();

            TopClass tc = new TopClass();
            tc.SomeJob();

            Console.ReadLine();
        }
    }

    public class BaseClass
    {
        public virtual void InvokeMethod()
        {
            Console.WriteLine("This is the base method");
        }
    }

    public class IntermediateClass : BaseClass
    {
        public override void InvokeMethod()
        {
            Console.WriteLine("This is the intermediate method");
        }

        public void IntermediateMethod()
        {
            // let's do another job here
            Console.WriteLine("Some job from Intermediate class level");
            InvokeMethod();
        }
    }

    public class TopClass : IntermediateClass
    {
        public override void InvokeMethod()
        {
            Console.WriteLine("This is the top method");
        }

        public void SomeJob()
        {
            // let's do some job here
            Console.WriteLine("Some job from Top class level");
            IntermediateMethod();
        }
    }

C# result
This is the base method
This is the intermediate method
Some job from Top class level
Some job from Intermediate class level
This is the top method

Advertisements

Method overriding in C# – part 2

As shown in the previous example a new keyword doesn’t guarantee that invoking of a method which is implemented in derived class will be possible, when reference is type of base class. To resolve this problem override keyword can be used.

C# code

class Program
    {
        static void Main(string[] args)
        {
            BaseClass bc = new DerivedClassTwo();
            bc.DisplayMessage();

            Console.ReadLine();
        }
    }

    public class BaseClass
    {
        public virtual void DisplayMessage()
        {
            Console.WriteLine("Base class message");
        }
    }

    public class DerivedClass : BaseClass
    {
        public override void DisplayMessage()
        {
            Console.WriteLine("Derived class message");
        }
    }

    public class DerivedClassTwo : DerivedClass
    {
        public override void DisplayMessage()
        {
            Console.WriteLine("Derived class two message");
        }
    }

C# result
Derived class two message

Method overriding in C# – part 1

Take a look at the example below. It doesn’t matter if we use a new keyword or not. As you can see reference bc will invoke the method of the class that matches its type (in this case BaseClass).

C# code

class Program
{
    static void Main(string[] args)
    {
        BaseClass bc = new DerivedClass();
        bc.DisplayMessage();

        Console.ReadLine();
    }
}

public class BaseClass
{
    public virtual void DisplayMessage()
    {
        Console.WriteLine("Base class message");
    }
}

public class DerivedClass : BaseClass
{
    public new void DisplayMessage()
    {
        Console.WriteLine("Derived class message");
    }
}

C# result
Base class message