C# Method Overriding


This entry is part 4 of 5 in the series C# Polymorphism

The post C# Introduction lists all of the C# series of posts we have at this site.

Method overriding is the process of modifying the implementation of an inherited method. Here we want to change the implementation of a method. We will use our own classes here. We will use a parent class and have child classes. All we will do in this example program is write a line of text to the console. The parent class has data and behaviour that is common to all of the child classes. The child classes do however have differences. An important concept here is encapsulation. Our child classes should have members that are specific to the characteristics of that particular child class. All of our child classes do something similar, in that they send some text to the console, but the text they send is not the same. Polymorphism means “many forms”.

In our Main() method we will have a list of our objects. We will Add() our child objects to the list. We will have another class that is responsible for displaying to the console. It will have a method that takes in as a parameter the list of our child objects. That method can therefore iterate over the list and for each of the child objects we can send a unique message to the console.

  • Base class – has one property and a WriteMessage method with no implementation
  • Main() method – create a list of colors and call WriteMessage of Display class
  • Display class – has method WriteMessage that takes List of base class items and iterates and calls WriteMessage
  • ChildRed – class that inherits from BaseClass and has WriteMessage method
  • ChildBlue – inherits from BaseClass and has WriteMessage method
  • adding more colors – add a new class and in Main() add the new class to the list – That’s It! Because that is all you need to do, we have achieved the loosely coupled objective.

Below is the screenshot of our output.

BaseClass.cs

namespace MethodOverriding
{
    class BaseClass
    {
        public int CommonProperty { get; set; }
        public virtual void WriteMessage()
        {
        }
    }
}

Program.cs

using System.Collections.Generic;
namespace MethodOverriding
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseclasses = new List<BaseClass>();
            baseclasses.Add(new ChildRed());
            baseclasses.Add(new ChildBlue());
            // baseclasses.Add(new ChildGreen() { CommonProperty = 7 });
            var display = new Display();
            display.WriteMessages(baseclasses);
        }
    }
}

Display.cs

using System.Collections.Generic;
namespace MethodOverriding
{
    class Display
    {
        public void WriteMessages(List<BaseClass> baseclasses)
        {
            foreach (var bc in baseclasses)
            {
                bc.WriteMessage();
            }
        }
    }
}

ChildRed.cs

using System;
namespace MethodOverriding
{
    class ChildRed : BaseClass
    {
        public override void WriteMessage()
        {
            Console.WriteLine("Red");
        }
    }
}

Loosely Coupled

This pattern is loosely coupled, which is what we want. When we need to add another color, all we need to do is add a new class and go to the Main() method and add that color to the list if we want. But that job of adding it to the list will be the job of the consumer of the code. So really, as developers of code, all we need to do is add another class and deploy that additional piece of code. The existing code that the user is currently using does not need to be re-deployed or updated.

Series Navigation<< C# Classes Polymorphism 3C# Method Overriding 2 >>