- C# Polymorphism Introduction
- C# Classes Polymorphism 2
- C# Classes Polymorphism 3
- C# Method Overriding
- C# Method Overriding 2
This is part 2 of our previous post called C# Method Overriding.
In this example we have added a lot of interesting code to the previous example. Here we are demonstrating polymorphism, method overriding, avoiding method overriding with the new keyword, the virtual keyword and the params keyword, among others.
- 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
- ChildGreen – inherits from BaseClass and has WriteMessage method and constructor, property and field
- ChildBlack – inherits from BaseClass and has WriteMessage method but no override
- adding more colors – add a new class and in Main() add the new class to the list – That’s It!
Below is the screenshot of our output.
BaseClass.cs
using System; namespace MethodOverriding2 { class BaseClass { public int CommonProperty { get; set; } public virtual void WriteMessage() { Console.WriteLine("Base class"); } public virtual void WriteMessage(params string[] strings) { Console.WriteLine("Your strings passed in to base class: "); foreach (string str in strings) { Console.Write($"|{str}|"); } Console.WriteLine(); } } }
Program.cs
using System.Collections.Generic; namespace MethodOverriding2 { class Program { static void Main(string[] args) { var baseclasses = new List<BaseClass>(); baseclasses.Add(new ChildRed()); baseclasses.Add(new ChildBlue() { CommonProperty = 4}); baseclasses.Add(new ChildGreen("grn_construct") { GreenProperty = "grn"}); baseclasses.Add(new ChildBlack()); var display = new Display(); display.WriteMessages(baseclasses); } } }
Display.cs is unchanged from the previous post.
using System.Collections.Generic; namespace MethodOverriding { class Display { public void WriteMessages(List<BaseClass> baseclasses) { foreach (var bc in baseclasses) { bc.WriteMessage(); } } } }
ChildRed.cs is unchanged from the previous post.
using System; namespace MethodOverriding { class ChildRed : BaseClass { public override void WriteMessage() { Console.WriteLine("Red"); } } }
ChildGreen.cs was added to this post.
using System; namespace MethodOverriding2 { class ChildGreen : BaseClass { private string GreenField = ""; public string GreenProperty { get; set; } public ChildGreen() { } // constructor public ChildGreen(string str) // constructor { GreenField = str; } public override void WriteMessage() { Console.WriteLine("Green " + GreenProperty + " " + GreenField); } } }
ChildBlack.cs was added to this post. The WriteMessage() method here is not called in our example. We could call it however. We would need to call it directly from an instance of the ChildBlack class.
using System; namespace MethodOverriding2 { class ChildBlack : BaseClass { new public void WriteMessage() { // this code is a bit unusual for this example... Console.WriteLine("Child Black not executed - no override - new"); } } }