16.12: Chapter 13
- Page ID
- 117508
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
13.1 Inheritance basics
- a. A doughnut is a type of pastry, so
Doughnutinherits fromPastry. - b. A kitchen has a freezer, so a
Kitchenclass would contain aFreezerinstance. A has-a relationship is also called a composition. - b. The
Goalkeeperclass inherits from thePlayerclass. - b. The subclass name is followed by the superclass name in parentheses.
- a. Deriving
CarryOnfromLuggageis specified inCarryOn's class definition. Ex:class CarryOn(Luggage):. Instances of a subclass, likeCarryOn, are created like other classes and do not require the base class name. - c. A
SubClassinstance has access tofunc_1(), inherited fromSuperClass, as well as tofunc_2(), aSubClassmethod. - a. A
SuperClassinstance has access toSuperClass'sfunc_1()but doesn't have access to anything inSubClass, includingfunc_2().
13.2 Attribute access
- b.
dev_1is the secondEmployeecreated. Sodev_1is the second object to callEmployee.__init__(), anddev_1.e_idis assigned with2. - a.
SubClass's__init__()is not explicitly defined, soSuperClass's__init__()is executed. - b.
dc_1inheritsfeat_1andfeat_2fromSuperClass. - b.
dev_display()is aDevelopermethod that accessesdev_1's instance attributelang_xp. - c.
dev_display()is aDevelopermethod.emp_1is not aDeveloperinstance, soemp_1cannot accessdev_display(). - a. Instance attributes inherited from
Employee, the superclass, are accessed the same way as the subclassDeveloper's instance attributes.
13.3 Methods
- b. Overriding involves defining a subclass method that has the same name as the superclass method.
- b. The self parameter is missing from the overridden method's definition.
- c. The class definition doesn't indicate inheritance. The definition should be
class ContractTax(Tax):. - b.
Square's superclass isRectangle.Rectangle's__init__()is called on line 6. - a.
super().__init__(4)calls the__init__()ofRectangle's superclass,Polygon. Control moves to line 3, andnum_sidesis initialized with4. - b. The instance attribute side is initialized with
5. - b.
Rectangle's__init__()always callsPolygon's__init__()withnum_sides=4. - c. Polymorphism allows
print_type()to be overridden in subclasses ofPolygon. - a. The * operator can be used with integer operands as well as strings and other types.
13.4 Hierarchical inheritance
- a.
Class Ais the superclass forClass BandClass C. - b. Employee would be an appropriate superclass for
DeveloperandSalesRep.DeveloperandSalesRepare not directly related. - a.
B(2)resolves toB(2, 0), asb_attrhas a default value of0.a_attris assigned with2. - b.
c_insthas access toC's instance attribute,c_attr, as well the inherited attribute fromA,a_attr. - b.
D, which inherits fromB, sod_instinheritsb_attr.Balso inherits fromA, sod_instinheritsd_attr.
13.5 Multiple inheritance and mixin classes
- a.
1is the argument passed toC's__init__()fora_attr. - b. An argument is not passed to
C's__init__()forc_attr, soc_attris assigned with the default value,20. - a.
2maps tob_attrinB's__init__(). - a.
eat()only applies to carnivorous plants, whichPitcherandVenusFlyTraprepresent. - b.
eat()can be moved into a mixin class included inPitcher's andVenusFlyTrap's inheritance list. - 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


