< Unit 3 - Arrays and Algorithms ('22-'23)

Lesson 7: Polymorphism

45 minutes

Overview

How can I create and use arrays of different objects?

Students explore arrays of objects and learn how to refer to object data when accessing elements in an array. Students revisit inheritance and discover using polymorphism to work with 1D arrays of a superclass type. Students review overriding methods and traverse an array of objects to use subclass methods.

CSA Conceptual Framework
      • MOD-3.B.12 - A subclass is usually designed to have modified (overridden) or additional methods or instance variables.
      • MOD-3.C.2 - If S is a subclass of T, then assigning an object of type S to a reference of type T facilitates polymorphism.
      • MOD-3.C.4 - Declaring references of type T, when S is a subclass of T, is useful in the following declarations: • Formal method parameters • arrays — T[] var ArrayList<T> var
      • MOD-3.D.2 - At compile time, methods in or inherited by the declared type determine the correctness of a non-static method call.
      • MOD-3.D.3 - At run-time, the method in the actual object type is executed for a non-static method call.
      • VAR-2.A.3 - Arrays can store either primitive data or object reference data.

Agenda

Objectives

Students will be able to:
  • Declare and initialize a one-dimensional (1D) array of a superclass type
  • Explain the purpose and benefits of polymorphism
  • Traverse a one-dimensional (1D) array of a superclass type and use subclass methods

Preparation

  • Print copies of the Investigating Polymorphism handout (one for each pair of students)
  • Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the students

Vocabulary

  • polymorphism - the ability of an object to perform an action in different ways

Teaching Guide

Warm Up (5 minutes)

Solve the Problem

Remarks

Each food truck at the Project Mercury Pastries Food Truck business sells a variety of desserts. The owner needs a way to quickly determine what desserts each food truck offers and apply discounts based on the specific type of dessert.

Do This: Review inheritance and the Project Mercury Pastries Food Truck classes and introduce the problem.

Discuss: How could we organize and work with the desserts offered at each food truck using 1D arrays?

Discussion Goal: Students suggest solutions for organizing the desserts into 1D arrays. Students may suggest creating an array for each type of Dessert or creating a single array that is of type Dessert that can store the different types of desserts.

Teaching Tip

Ask students to consider the benefits and drawbacks of their solutions. For example, if students suggest creating arrays for each type of Dessert, ask students about the efficiency and maintainability of this approach. If students suggest creating a single Dessert array, ask students how they would refer to a method to apply a discount differently for each type of dessert. Encourage students to recall how the toString() method was overridden and how overriding a method might be helpful in this scenario.

Activity (35 minutes)

Exploring Polymorphism (15 minutes)

Remarks

Arrays can store primitive values and references to objects. We can create an array of desserts, painters, and any other type of object in our programs.

Do This: Review the lesson objectives.

Do This: Direct students to Level 1 on Code Studio to predict the program's outcome, then run the program to compare their predictions to the actual outcome.

Discuss: Click through the animated slide to display the prompts.

  • What do you notice about the code in this program?
  • What do you wonder about the code in this program?

Discussion Goal: Students notice that the 1D array stores references to objects and that they can call an object's method while traversing the array. Students may wonder how they could create an array of different types of objects that extend the same superclass and whether calling each object's method will call the subclass version while traversing the array.

Do This: Click through the animated slide to review the default values for a 1D array that is initialized to contain primitive values.

Do This: Click through the animated slide to discuss that the elements in a 1D array that is initialized to contain reference values are initialized to null.

Group: Place students in pairs.

Distribute: Give each pair a copy of the Investigating Polymorphism handout.

Do This: Direct students to Level 2 on Code Studio. Students work with their partners to explore the program on Level 2 and complete the Investigating Polymorphism handout.

Using Polymorphism

Remarks

Creating arrays of objects unlock new capabilities. We can make 1D arrays of a superclass type and access subclass versions of behaviors as we traverse the array. Creating and using arrays in this way is an object-oriented programming concept called polymorphism.

Display: Show the video – Polymorphism.

Do This: Define polymorphism and explain its functionality.

  • Define polymorphism
  • Explain the relationship between the superclass and its subclasses
  • Explain how each subclass has its version of the method and that this is an example of overriding.
  • Demonstrate the result of calling each subclass version of the method
  • Explain that an array of the superclass type can store objects of a superclass or subclass type
Teaching Tip

Point out that when the code is compiled, Java checks that the method exists in either the superclass or subclass. When the code is run, Java uses the method in the subclass if it exists or the method in the superclass.

Discuss: Click through the animated slide to display the prompts. Use the Hold That Thought strategy to discuss the prompts.

  • What are the benefits of using polymorphism in a program?
  • When might you use polymorphism in a program?

Discussion Goal: Students note that polymorphism can help make their code more flexible and that they can have a single array to represent multiple types of objects. Students suggest using polymorphism to create an array of Dessert objects, such as Cookie and Pie objects.

Teaching Tip

Prompt students to consider scenarios where they might have a list of a generic type that contains more specific items, such as a grocery list or a playlist containing different music genres. Ask students how polymorphism might make it easier to manage these lists.

Do This: Direct students to Level 3 on Code Studio to complete Levels 3 and 4. Students complete a Check for Understanding, then continue to Level 4 to complete a choice level to use polymorphism.

Wrap Up (5 minutes)

Revisiting the Food Truck

Remarks

Let's see if we can use what we learned today to help the Project Mercury Pastries Food Truck business!

Discuss: Click through the animated slide to display the prompts.

  • These are the desserts that one food truck sells. How should we store these desserts in our program?
  • Each dessert has its own special discount. What is the most efficient way to apply the discounts to each Dessert object in the array?

Discussion Goal: Students suggest creating a 1D array of type Dessert to store each type of dessert. Students also suggest that each Dessert subclass have a method to apply discount so that the methods can be called when traversing the array of Dessert objects.

Do This: Review the concepts covered in this lesson.

Display: Key Vocabulary


Assessment: Check for Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

AP Classroom Topic Questions

To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.

The following Topic Questions in AP Classroom can be assigned as a formative assessment for this lesson:

  • Topic Questions 9.3
  • Topic Questions 9.5
  • Topic Questions 9.6

Note: Some Learning Objectives and Essential Knowledge statements in the suggested Topic Questions are covered in later units.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.