Computer Science Grade 9 20 min

Polymorphism: Working with Objects of Different Types

Explore polymorphism and how it allows you to treat objects of different classes in a uniform way.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define polymorphism in the context of object-oriented programming. Explain the relationship between inheritance and polymorphism. Create a superclass and at least two subclasses that inherit from it. Implement method overriding in a subclass to provide specialized behavior. Write code that uses a superclass reference to manage a collection of different subclass objects. Trace the execution of a program that demonstrates polymorphic behavior. Have you ever used the same button on a game controller to do different things, like 'use' a key to open a door or 'use' a potion to heal? 🎮 That's polymorphism in action! In this lesson, you will learn about polymorphism, a powerful concept that allows us to treat objects of different class...
2

Key Concepts & Vocabulary

TermDefinitionExample PolymorphismLiterally meaning 'many forms', it's the ability of an object to take on many forms. In programming, it means we can use a single interface (like a method name) to represent different underlying forms (different method implementations).A list of `Animal` objects can hold a `Dog` object, a `Cat` object, and a `Bird` object. When we tell each one to `make_sound()`, the dog barks, the cat meows, and the bird chirps. Superclass (Parent Class)A general class that other, more specific classes can inherit from. It defines common attributes and methods.The `Animal` class is a superclass. It might have a `name` attribute and a `make_sound()` method that all animals share. Subclass (Child Class)A specific class that inherits attributes and methods fr...
3

Core Syntax & Patterns

The Inheritance Pattern class SubclassName(SuperclassName): # Subclass-specific code here Use this syntax to establish an 'is-a' relationship. The `SubclassName` will automatically gain access to the methods and attributes of the `SuperclassName`, forming the foundation for polymorphism. The Method Overriding Pattern class SubclassName(SuperclassName): def method_to_override(self): # New, specific implementation To override a method, define a method in the subclass with the exact same name and parameters as the one in the superclass. This allows the subclass object to perform the action in its own unique way. The Polymorphic Collection Pattern my_list = [Subclass1(), Subclass2(), Subclass3()] for item in my_list: item.shared_method() You...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
A student avoids polymorphism by writing this code: `for item in shape_list: if type(item) is Circle: item.draw_circle() elif type(item) is Rectangle: item.draw_rectangle()`. Why is the polymorphic approach with a single `draw()` method superior?
A.The student's code is faster.
B.The polymorphic approach is the only way to use a list.
C.The student's code requires modification every time a new shape subclass (e.g., `Triangle`) is added.
D.The student's code uses less memory.
Challenging
You are designing a game where different weapons (`Sword`, `Axe`, `Bow`) all inherit from a `Weapon` class. The `Weapon` class has a `use_weapon()` method. You want to add a new `MagicWand` class. What is the minimum you must do for a `MagicWand` object to work correctly in the existing game loop: `for weapon in player_inventory: weapon.use_weapon()`?
A.Create the `MagicWand` class and give it a unique `cast_spell()` method.
B.Make the `MagicWand` class inherit from `Weapon` and implement its own `use_weapon()` method.
C.Make the `MagicWand` class inherit from `Weapon`, but do not add any methods.
D.Rewrite the game loop to check for the `MagicWand` type.
Challenging
Consider a superclass `MediaFile` with a `play()` method. Subclasses are `AudioFile` and `VideoFile`. `AudioFile` overrides `play()`. `VideoFile` has a unique method `show_subtitles()`. A list contains both types: `playlist = [AudioFile(), VideoFile()]`. Which statement is true?
A.loop can call `play()` on both objects, but cannot call `show_subtitles()` on the `VideoFile` without a type check.
B.loop can call `show_subtitles()` on both objects.
C.loop calling `play()` will use the `MediaFile` implementation for both objects.
D.The `playlist` is invalid because `VideoFile` has a method that `AudioFile` does not.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming: Designing with Classes and Objects

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.