What is Existential Crisis?

What is the definition of existential crisis, anyway? This has become a very popular topic on the Internet these days, since many psychologists have begun offering “explanatory” treatments for those…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Teaching Polymorphism and Inheritance with Mario Kart

Polymorphism meaning many(poly) forms(morph) is at the heart of object-oriented programming (OOP) which is generally taught in the second semester of AP Computer Science A course.

When we talk to teachers on how polymorphism and inheritance is taught in his or her programming class, the story seems to split into two groups. One group identifies it as one of the easiest concepts covered in the course — a breeze, while the other sees it as a difficult concept that students struggle with the most.

Regardless of how it is received, both groups find it tough to find good examples besides Mammal → Dog, Mammal → Cat.

The animal classification is a simple, universally-understood metaphor but doesn’t quite capture the usefulness and the power of polymorphism in programming software.

In this article, we look at polymorphism and inheritance in a popular game called the Mario Kart and how it can be introduced to the students as an intro activity.

*This activity can be done with other games of your choice.

When playing games such as Mario Kart, Grand Theft Auto, or Pokemon Go, the player must first choose the main character from the given roster of characters. The characters have their similarities but I’m sure everyone has his or her favorites in the big pool of characters. It may be the looks. It may be the performance that makes this character so attractive. Whatever it is that draws you, the characters are somewhat same and at the same time different!

Question: What are alike? What is different?

These characters all have common attributes such as weight, speed, acceleration, and handling with varying strengths and weaknesses. A lighter kart is slower but have better handling giving advantages in cornering. A heavier kart is faster but have lousy handling.

Mario Kart Character stats By Henry Hinnefield

What other common features can you find among these characters? The characters all use the same key commands for moving and using items. Another trait they share is their behavior. When bumping into other karts or obstacles, they bounce, swerve, and spin.

The common attributes keep the characters consistent and their behaviors somewhat predictable. However, each character has its own unique characteristics that distinguishes Princess Peach from Wario.

2. Try creating a class table of a Player Class

The characters share a common attribute. So it is easy to imagine that there must exist a template that these characters use to build themselves from. Let’s name this template class ‘Player’ and come up with a class table. The individual characters will inherit the template class and build itself from it.

Remind students that when filling out the class table, we must include instance variables (nouns) and methods(verbs). Note that this is an open-ended exercise so encourage students to come up with as many attributes as possible.

3. Try creating a subclass PeachPlayer.

The template Player class is also called the superclass or the parent class. The parent class can have child classes or subclasses inheriting its attributes. This means that the subclasses such as Mario, Luigi, Peach, and other Mario Kart characters can use the Player class to build itself.

Did you know that there are variations of Princess Peach on Mario Kart? There are four versions of Peach! What do you think this means? This means that Princess Peach can also serve as a superclass of other subclasses such as Cat Peach, Pink Gold Peach, and Baby Peach.

Now the Peach Class that extends the Superclass Player can use all the attributes of the Superclass and additionally, have its own attributes. Cool!

4. What other components of the game would benefit from having a template (superclass)?

In Mario Kart, the main player character isn’t the only thing that takes advantage of polymorphism. Can you guess what other components of the game would benefit from creating a template (superclass)?

The race tracks would be one. Think of the map and the various textures of the tracks. What about the special items such as the Banana, Green Shell, and Super Star?

We hope that this intro can get your students interested and excited for Polymorphism before rolling up their sleeves for coding.

More Resources:

Add a comment

Related posts:

Is Curation On Its Way Out?

I joined Medium in November 2018 as a reader, then signed up with the Member Partner Program soon after. The Medium company has made some big changes since then, e.g. MPP payments were calculated…

How to create a Fidia website in 5 simple steps

Creating a Fidia website is one of the simplest things you can do on Fidia. If you are wondering how to create one, these 5 simple steps will save time and stress. You should first log in to your…

What are the reasons behind Marriage Problems?

When you get married you create an ideal marriage in your mind, ‘My marriage will be like this and like that’, but gradually your picture of an ideal marriage remains, just that, a picture. So what…