Elf header

Welcome to Lesson 3

You are at the halfway mark on your OOP journey! In this lesson, Steve will introduce you to the concept of polymorphism. You'll see how methods (or behaviors) can have handle multiple implemtations of an action and still work seamlessly in our game.

You'll learn about:

  • how polymorphism can cut down on redundant code
  • examples of how our game uses polymorphism
  • overloading and how it applies to polymorphism

Overload Elf's Attack

In this lesson Steve showed us how our character's attack behaviors can accomodate many types of weapons, a concept known as polymorphism. This is different than overriding, a concept we learned about in Lesson 2. Overriding is where you replace a specific data type, for example, upgrading a characters weapon. Overloading lets us add to attack by overloading the attack behavior with more weapon options. Be sure to change your base class to make sure the overloaded attack still works!

Let's Recap.

Some new terms were presented in this lesson. Have a look before you take the quiz!


POLYMORPHISM

Polymorphism refers to a programming languages ability to process objects differently based on their data type or class. All of our player characters have an attack behavior. We can simply say "elf, attack!" and based on the weapon that elf instance has, the elf will attack. In our game, when we push the attack button, the game says "character, attack." Polymorphism makes it possible for this behavior to work no matter what character we have selected as our player.


OVERLOAD

Overloading refers to the ability to use a single method to define multiple behaviors of a class. Overloaded behaviors are generally used when they execute the same task but with a slightly different outcome. We overloaded the Elf's attack behavior to accomodate many types of weapons.


Test Your Knowledge.

Are you ready to see what you've learned? Take the quiz below to see if your ready to move on to the next lesson!

Take a break - You've earned it!

In this lesson we learned about the ability of an object to take on many forms in a program, a concept known as polymorphism. Before moving on to the final lesson, think about everyday object behaviors that could be overloaded to accomplish an action in different ways. Steve used the example of an HDMI input on a television that was overloaded by a cable, computer and game console signal. Can you think of another example? Visit Lesson 3 discussion board and give your example, or ask a question!

Ready for Lesson 4?