13.4: Hierarchical Inheritance
- Page ID
- 117609
\( \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}}}\)
By the end of this section you should be able to
- Label relationships between classes as types of inheritance.
- Construct classes that form hierarchical inheritance.
Hierarchical inheritance basics
Hierarchical inheritance is a type of inheritance in which multiple classes inherit from a single superclass. Multilevel inheritance is a type of inheritance in which a subclass becomes the superclass for another class. Combining hierarchical and multilevel inheritance creates a tree-like organization of classes.
Which is an example of hierarchical inheritance?
- Class B inherits from Class A
Class C inherits from Class A - Class B inherits from Class A
Class C inherits from Class B - Class B inherits from Class A
Class C inherits from Class D
- Answer
-
a.
Class Ais the superclass forClass BandClass C.
Which group of classes is hierarchical inheritance appropriate for?
Cat,Dog,BirdEmployee,Developer,SalesRepDessert,BakedGood,ApplePie
- Answer
-
b. Employee would be an appropriate superclass for
DeveloperandSalesRep.DeveloperandSalesRepare not directly related.
Implementing hierarchical inheritance
Multiple classes can inherit from a single class by simply including the superclass name in each subclass definition.
class ChoirMember:
def display(self):
print("Current choir member")
class Soprano(ChoirMember):
def display(self):
super().display()
print("Part: Soprano")
class Soprano1(Soprano):
def display(self):
super().display()
print("Division: Soprano 1")
class Alto(ChoirMember):
def display(self):
super().display()
print("Part: Alto")
class Tenor(ChoirMember):
def display(self):
super().display()
print("Part: Tenor")
class Bass(ChoirMember):
def display(self):
super().display()
print("Part: Bass")
mem_10 = Alto()
mem_13 = Tenor()
mem_15 = Soprano1()
mem_10.display()
print()
mem_13.display()
print()
mem_15.display()
The code's output is:
Current choir member
Part: Alto
Current choir member
Part: Tenor
Current choir member
Part: Soprano
Division: Soprano 1
Consider the program:
class A:
def __init__(self, a_attr=0):
self.a_attr = a_attr
class B(A):
def __init__(self, a_attr=0, b_attr=0):
super().__init__(a_attr)
self.b_attr = b_attr
class C(A):
def __init__(self, a_attr=0, c_attr=0):
super().__init__(a_attr)
self.c_attr = c_attr
class D(B):
def __init__(self, a_attr=0, b_attr=0, d_attr=0):
super().__init__(a_attr, b_attr)
self.d_attr = d_attr
b_inst = B(2)
c_inst = C(c_attr=4)
d_inst = D(6, 7)
What is the value of b_inst.b_attr?
02Error
- Answer
-
a.
B(2)resolves toB(2, 0), asb_attrhas a default value of0.a_attris assigned with2.
Which attributes does c_inst have access to?
a_attra_attr,c_attrc_attr
- Answer
-
b.
c_insthas access toC's instance attribute,c_attr, as well the inherited attribute fromA,a_attr.
Which attributes does d_inst have access to?
b_attr,d_attra_attr,b_attr,d_attrd_attr
- Answer
-
b.
D, which inherits fromB, sod_instinheritsb_attr.Balso inherits fromA, sod_instinheritsd_attr.
Define three classes: Instrument, Woodwind, and String.
Instrumenthas instance attributeowner, with default value of "unknown".Woodwindinherits fromInstrumentand has instance attributematerialwith default value of "wood".Stringinherits fromInstrumentand has instance attributenum_strings, with default value of 4.
The output should match:
This flute belongs to unknown and is made of silver
This cello belongs to Bea and has 4 strings
Interactive Code
- Answer
-
class Instrument():
def __init__(self, name='unknown'):
self.owner = nameclass Woodwind(Instrument):
def __init__(self, name='unknown', material='wood'):
super().__init__(name)
self.material = materialclass String(Instrument):
def __init__(self, name='unknown', num_strings=4):
super().__init__(name)
self.num_strings = num_stringsflute_1 = Woodwind(material='silver')
cello_1 = String('Bea')print(f"This flute belongs to {flute_1.owner} and is made of {flute_1.material}")
print(f"This cello belongs to {cello_1.owner} and has {cello_1.num_strings} strings")


