Section16.17Group 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 .
Note16.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
Subsection16.17.1Inheritance
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.
Checkpoint16.17.2.
Q-1: What is the last thing that the following code will print?
Checkpoint16.17.3.
Run the following code
Look at the classes defined above. Then answer the following questions.
Checkpoint16.17.4.
Q-3: How many attributes does an employee object have?
Checkpoint16.17.5.
Q-4: Which class defines the initials method?
Checkpoint16.17.6.
Q-5: Which class is the child class?
Checkpoint16.17.7.
Drag the code to the description of what it does
Read the chapter on strings and try again
super().__init__(first, last)
Passes the first and last name to the parent class's __init__ method
class Employee(Person):
Defines a new class that inherits from the Person class
def __init__(self, first, last):
Defines an initializer (constructor) method
def __str__(self):
Defines a method that is called when you print an object of this class
Checkpoint16.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.
class Car:
---
def __init__(self, make):
---
self.make = make
---
def __str__(self):
---
def __str__(): #paired
---
return "Car with make: " + self.make
---
class SportsCar(Car):
---
class SportsCar extends Car: #paired
---
def __init__(self, make):
---
super().__init__(make)
---
super().__init__(self, make) #paired
Look at the code below and answer the following questions.
Checkpoint16.17.9.
Q-8: What is the first thing that the following code will print?
Checkpoint16.17.10.
Q-9: What is the last thing that the following code will print?
Checkpoint16.17.11.
Run the following code
Checkpoint16.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.
Subsection16.17.2Overriding 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.
Checkpoint16.17.13.
Q-12: What is the first thing that will be printed when the code runs below?
Checkpoint16.17.14.
Q-13: What is the last thing that will be printed when the code runs below?
Checkpoint16.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>