Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

13.1 Inheritance basics

1.
a. A doughnut is a type of pastry, so Doughnut inherits from Pastry.
2.
b. A kitchen has a freezer, so a Kitchen class would contain a Freezer instance. A has-a relationship is also called a composition.
3.
b. The Goalkeeper class inherits from the Player class.
4.
b. The subclass name is followed by the superclass name in parentheses.
5.
a. Deriving CarryOn from Luggage is specified in CarryOn's class definition. Ex: class CarryOn(Luggage): . Instances of a subclass, like CarryOn, are created like other classes and do not require the base class name.
6.
c. A SubClass instance has access to func_1(), inherited from SuperClass, as well as to func_2(), a SubClass method.
7.
a. A SuperClass instance has access to SuperClass's func_1() but doesn't have access to anything in SubClass, including func_2().

13.2 Attribute access

1.
b. dev_1 is the second Employee created. So dev_1 is the second object to call Employee.__init__(), and dev_1.e_id is assigned with 2 .
2.
a. SubClass's __init__() is not explicitly defined, so SuperClass's __init__() is executed.
3.
b. dc_1 inherits feat_1 and feat_2 from SuperClass.
4.
b. dev_display() is a Developer method that accesses dev_1's instance attribute lang_xp.
5.
c. dev_display() is a Developer method. emp_1 is not a Developer instance, so emp_1 cannot access dev_display().
6.
a. Instance attributes inherited from Employee, the superclass, are accessed the same way as the subclass Developer's instance attributes.

13.3 Methods

1.
b. Overriding involves defining a subclass method that has the same name as the superclass method.
2.
b. The self parameter is missing from the overridden method's definition.
3.
c. The class definition doesn't indicate inheritance. The definition should be class ContractTax(Tax):.
4.
b. Square's superclass is Rectangle. Rectangle's __init__() is called on line 6.
5.
a. super().__init__(4) calls the __init__() of Rectangle's superclass, Polygon. Control moves to line 3, and num_sides is initialized with 4 .
6.
b. The instance attribute side is initialized with 5 .
7.
b. Rectangle's __init__() always calls Polygon's __init__() with num_sides=4.
8.
c. Polymorphism allows print_type() to be overridden in subclasses of Polygon.
9.
a. The * operator can be used with integer operands as well as strings and other types.

13.4 Hierarchical inheritance

1.
a. Class A is the superclass for Class B and Class C.
2.
b. Employee would be an appropriate superclass for Developer and SalesRep. Developer and SalesRep are not directly related.
3.
a. B(2) resolves to B(2, 0), as b_attr has a default value of 0 . a_attr is assigned with 2 .
4.
b. c_inst has access to C's instance attribute, c_attr, as well the inherited attribute from A, a_attr.
5.
b. D, which inherits from B, so d_inst inherits b_attr. B also inherits from A, so d_inst inherits d_attr.

13.5 Multiple inheritance and mixin classes

1.
a. 1 is the argument passed to C's __init__() for a_attr.
2.
b. An argument is not passed to C's __init__() for c_attr, so c_attr is assigned with the default value, 20 .
3.
a. 2 maps to b_attr in B' s __init__().
4.
a. eat() only applies to carnivorous plants, which Pitcher and VenusFlyTrap represent.
5.
b. eat() can be moved into a mixin class included in Pitcher's and VenusFlyTrap's inheritance list.
6.
b. Good practice is to have a mixin class name end in "Mixin" to identify the class's purpose. The final implementation is:
class Plant:
  def photosynth(self):
    print("Photosynthesizing")

class CarnivMixin:
  def eat(self):
    print("Eating")

class Rose(Plant):
  pass

class Pitcher(Plant, CarnivMixin):
  pass

class VenusFlyTrap(Plant, CarnivMixin)
  pass
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Feb 26, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.