Polymorphism - C# Academy: Learn the Basics of C# (2014)

C# Academy: Learn the Basics of C# (2014)

Chapter 6. Polymorphism

And of course, there’s also Polymorphism, which is also known as an important pillar of object-oriented programming. It has two essential aspects—apart from its many“shapes”, and these are:

1. Virtual Methods cannot be implemented or defined through base classes because they can be derived by override classes, and can even be implemented or defined on its own. When the Client Code calls the method during runtime, you have to make sure that the CLR is able to look back on the project.

2. Method Parameters and Collection Arrays should be treated as objects from one derived class, and should not be identical to its run-type in any way.

Therefore, you can work with various groups of objects in a way that would be coherent with one another. For example, if you want to create an app that would teach kids what different shapes are, you have to make sure that you add different shapes to the program—even before the editing period. Should you encounter any problems, you just have to do the following:

1. Use virtual methods to invoke the necessary methods for classes that have been derived through single call methods.

2. Create class hierarchies with specific shapes from a common base class.

Here’s a good example:

public class Shapes App

{

// A few example members

public int X { get; private set; }

public int Y { get; private set; }

public int Height { get; set; }

public int Width { get; set; }

// Virtual method

public virtual void Draw()

{

Console.WriteLine("Performing base class drawing tasks");

}

}

class Heart : Shape

{

public override void Draw()

{

// Code to draw a heart...

Console.WriteLine("Drawing a heart");

base.Draw();

}

}

class Oblong : Shape

{

public override void Draw()

{

// Code to draw a oblong...

Console.WriteLine("Drawing a oblong");

base.Draw();

}

}

class Diamond : Shape

{

public override void Draw()

{

// Code to draw a diamond...

Console.WriteLine("Drawing a diamond");

base.Draw();

}

}

class Program

{

static void Main(string[] args)

{

// Polymorphism at work #1: an Oblong, Diamond and Heart

// can all be used whereever a Shape is expected. No cast is

// required because an implicit conversion exists from a derived

// class to its base class.

System.Collections.Generic.List<Shape> shapes = new System.Collections.Generic.List<Shape>();

shapes.Add(new Oblong());

shapes.Add(new Diamond());

shapes.Add(new Heart());

// Polymorphism at work #2: the virtual method Draw is

// invoked on each of the derived classes, not the base class.

foreach (Shape s in shapes)

{

s.Draw();

}

// Keep the console open in debug mode.

Console.WriteLine("Press any key to exit.");

Console.ReadKey();

}

}

/* Output:

Drawing an oblong

Performing base class drawing tasks

Drawing a diamond

Performing base class drawing tasks

Drawing a heart

Performing base class drawing tasks

*/

You could also use Polymorphism to hide base class members—or members of the class that you no longer want to see in your program. Take note that client code could still have access to this:

public class Hogwarts

{

public void DoWork() { WorkField++; }

public int Gryffindor;

public int Slytherin

{

get { return 0; }

}

}

public class DerivedClass : Gryffindor

{

public new void DoWork() { WorkField++; }

public new int Gryffindor;

public new int Slytherin

{

get { return 0; }

}

}