Skip to main content

Section 16.17 Group Work - Working with Multiple Classes

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home 1 .

Note 16.17.1.

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
  • Define a parent and child class.
  • Predict the output from code with inheritance.
  • Describe how a child can override an inherited method.
  • Call the parent constructor

Subsection 16.17.1 Inheritance

One class can inherit attributes and behavior from another class. The class that is inherited from is called the parent or base class. The one that is inheriting is called the child or derived class.

Checkpoint 16.17.2.

Q-1: What is the last thing that the following code will print?

Checkpoint 16.17.3.

Run the following code
Look at the classes defined above. Then answer the following questions.

Checkpoint 16.17.4.

Q-3: How many attributes does an employee object have?

Checkpoint 16.17.5.

Q-4: Which class defines the initials method?

Checkpoint 16.17.6.

Q-5: Which class is the child class?

Checkpoint 16.17.7.

Checkpoint 16.17.8.

Drag the blocks from the left and put them in the correct order on the right to define two classes: Car and SportsCar that inherits from Car. Add an __init__ method to the Car class that takes a make and sets the current object's make. Add a __str__ method to the Car class and return a string with the make. Add an __init__ method to the SportsCar class that calls the parent class __init__ method.
Look at the code below and answer the following questions.

Checkpoint 16.17.9.

Q-8: What is the first thing that the following code will print?

Checkpoint 16.17.10.

Q-9: What is the last thing that the following code will print?

Checkpoint 16.17.11.

Run the following code

Checkpoint 16.17.12.

    Q-11: What happens when a class doesn't have a __str__ method and you print an object?
  • You get an error
  • It won't cause an error
  • It prints None
  • It doesn't print None
  • It prints the type of the object (the class name)
  • It prints the type of the object (the class name)
  • It prints the variable name
  • It does not print the variable name
Modify the code above to add a __str__ method to the Greeter class that returns the self.message and run the code again.

Subsection 16.17.2 Overriding an Inherited Method

The child class can override an inherited method. This means it provides the same method name and parameter list as an inherited method.

Checkpoint 16.17.13.

Q-12: What is the first thing that will be printed when the code runs below?

Checkpoint 16.17.14.

Q-13: What is the last thing that will be printed when the code runs below?

Checkpoint 16.17.15.

Run the following code
If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.
<div class="runestone sqcontainer %(optclass)s"> <div data-component="groupsub" id=mult_classes_groupsub data-size_limit=3> <div class="col-sm-6"> <select id="assignment_group" multiple class="assignment_partner_select" style="width: 100%"> </select> </div> <div id="groupsub_button" class="col-sm-6"> </div> <p>The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.</p> </div> </div>
https://cspogil.org/Home