Polymorphism in C#

Course Home | What is Polymorphism? | Code Example(s)

So I don’t have much to add the explanation I gave in the Programming Basics course. So let’s just look at some code with comments.

using System;
using CSharpBasics.anothernamespace;
using NUnit.Framework;

namespace CSharpBasics.lessons
{
    //We covered a lot of this logic in the Encapsulation examples

    //I've created two Classes which are in the 'anotherpackage' package. Just to keep the javalessons in order, no other reason
    //SubClassWithWaitMethod which extends SuperClassWithWaitMethod
    //Both has a method on them called ConsoleMethod()
    //Both write to the console 'I'm a method on the <ClassName> class'
    //The test below should use the one from the SubClassWithWaitMethod as that overwrites the SuperClass
    //So in the console we'll see "I'm a method on the SubClassWithWaitMethod class"

    public class L_Polymorphism
    {
        [Test]
        public void CallingWaitMethod()
        {
            SubClassWithWaitMethod polymorphism = new SubClassWithWaitMethod();
            polymorphism.ConsoleMethod();
        }
    }   

}

Now I must admit I don’t use polymorphism a lot. Again the cases I’ve used this a lot is with PageObjects. Again in those instances, it’s to overwrite a wait method. But again I’m not including these things because you need to know them, it’s so you have more options when you come to design code. But also so you have a better understanding when reading others code.

Next Lesson ➔

Richard Bradshaw's photo
Author

Richard Bradshaw

@FriendlyTester

Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.

Comments