Polymorphism in C#: an overview

Polymorphism means "many forms". An object can be seen in more than one way, and code can work with whichever way it needs.

Apprentice · C# · OOP · Updated

After this lesson you can

  • Explain polymorphism as one object seen in many forms

Key takeaways

  • Polymorphism means "many forms": one object can be seen in more than one way, as Chloe is a student, a singer and an athlete.
  • An archer is a character, so an Archer object can be seen as an Archer or simply as a Character.
  • Code that only cares about characters can treat an archer, a wizard and a knight the same way, without knowing which kinds exist.

Polymorphism sounds like a big word, but the idea is an everyday one. The word has two parts:

  • poly, meaning many,
  • morph, meaning forms.

A polymorphic object is one that can be seen in many forms. That doesn't mean it changes shape like a wizard in a story. It means different people, or different code, can look at the same object in different ways.

One person, many roles

Take Chloe, a high school student. Looking at her, you know she's a person, and not much more.

Chloe, a high school student

But Chloe is a math scholar, she sings in the school plays, and she plays on the volleyball team.

Chloe is a scholar, a singer and an athlete

She's one person, in many forms: a student to her teacher, a singer to the drama club, an athlete to her coach.

Many forms in code

You've already met this in C#. An archer is a character. So an Archer object can be seen in two forms:

  • as an Archer, with arrows and everything else an archer has,
  • or simply as a Character, like any other character in the game.

Code that only cares about characters (a battle, a party list, a save file) can treat an archer, a wizard and a knight all as characters, and work with all three the same way. That's polymorphism, and the next lessons show what it looks like.

Check yourself

1 question to try on the lesson page.

So what

Polymorphism lets code see an object in the form it needs. One loop can handle every kind of character, without knowing or caring which kinds exist.

Key terms

Cheat sheets