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.
Note
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
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.
csp-10-2-1: What is the last thing that the following code will print?
Run the following code
Look at the classes defined above. Then answer the following questions.
csp-10-2-3: How many attributes does an employee object have?
csp-10-2-4: Which class defines the initials method?
csp-10-2-5: Which class is the child class?
-
csp-10-2-6: 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
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.
csp-10-2-8: What is the first thing that the following code will print?
csp-10-2-9: What is the last thing that the following code will print?
Run the following code
- 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
csp-10-2-11: What happens when a class doesn’t have a __str__ method and you print an object?
Modify the code above to add a __str__ method to the Greeter class that returns the self.message and run the code again.
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.
csp-10-2-12: What is the first thing that will be printed when the code runs below?
csp-10-2-13: What is the last thing that will be printed when the code runs below?
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.
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.