Key Concepts

Review core concepts you need to learn to master this subject

C# Polyphormism

class Novel : Book { public override string Stringify() { return "This is a Novel!; } } class Book { public virtual string Stringify() { return "This is a Book!; } } // In the below code, you’ll see that a Novel and Book object can both be referred to as Books. This is one of their shared interfaces. At the same time, they are different data types with unique functionality. Book bk = new Book(); Book warAndPeace = new Novel(); Console.WriteLine(bk.Stringify()); Console.WriteLine(warAndPeace.Stringify()); // This is a Book! // This is a Novel // Even though bk and warAndPeace are the same type of reference, their behavior is different. Novel overrides the Stringify() method, so all Novel objects (regardless of reference type) will use that method.

Polymorphism is the ability in programming to present the same interface for different underlying forms (data types).

We can break the idea into two related concepts. A programming language supports polymorphism if:

  1. Objects of different types have a common interface (interface in the general meaning, not just a C# interface), and
  2. The objects can maintain functionality unique to their data type