13.1: Inheritance Basics
- Page ID
- 117606
\( \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{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)By the end of this section you should be able to
- Identify is-a and has-a relationships between classes.
- Differentiate between a subclass and a superclass.
- Create a superclass, subclass, and instances of each.
is-a vs has-a relationships
Classes are related to each other. An is-a relationship exists between a subclass and a superclass. Ex: A daffodil is a plant. A Daffodil class inherits from a superclass, Plant.
Is-a relationships can be confused with has-a relationships. A has-a relationship exists between a class that contains another class. Ex: An employee has a company-issued laptop. Note: The laptop is not an employee.
What is the relationship between a Doughnut class and a Pastry class?
- is-a
- has-a
- Answer
-
a. A doughnut is a type of pastry, so
Doughnutinherits fromPastry.
What is the relationship between a Kitchen class and a Freezer class?
- is-a
- has-a
- Answer
-
b. A kitchen has a freezer, so a
Kitchenclass would contain aFreezerinstance. A has-a relationship is also called a composition.
A goalkeeper is a player. Goalkeeper is a ______class. Player is a ______class.
- super; sub
- sub; super
- Answer
-
b. The
Goalkeeperclass inherits from thePlayerclass.
Inheritance in Python
Inheritance uses an is-a relationship to inherit a class from a superclass. The subclass inherits all the superclass's attributes and methods, and extends the superclass's functionality.
In Python, a subclass is created by including the superclass name in parentheses at the top of the subclass's definition:
class SuperClass:
# SuperClass attributes and methods
class SubClass(SuperClass):
# SubClass attributes and methods
How is a Daisy class that inherits from the Plant class defined?
-
class Plant(Daisy): class Daisy(Plant): class Daisy: class Plant:
- Answer
-
b. The subclass name is followed by the superclass name in parentheses.
Suppose a CarryOn class is inherited from a Luggage class. How is a CarryOn instance created?
small_bag = CarryOn()small_bag = Luggage(CarryOn)small_bag = CarryOn(Luggage)
- Answer
-
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.
Given the following SuperClass and SubClass, which of the following can an instance of SubClass access?
class SuperClass():
def func_1(self):
print('Superclass function')
class SubClass(SuperClass):
def func_2(self):
print('Subclass function')
func_1()onlyfunc_2()onlyfunc_1()andfunc_2()
- Answer
-
c. A
SubClassinstance has access tofunc_1(), inherited fromSuperClass, as well as tofunc_2(), aSubClassmethod.
Given the following SuperClass and SubClass, which of the following can an instance of SuperClass access?
class SuperClass():
def func_1(self):
print('Superclass function')
class SubClass(SuperClass):
def func_2(self):
print('Subclass function')
func_1()onlyfunc_2()onlyfunc_1()andfunc_2()
- Answer
-
a. A
SuperClassinstance has access toSuperClass'sfunc_1()but doesn't have access to anything inSubClass, includingfunc_2().
Python documentation for inheritance uses multiple terms to refer to the class that is inherited from and the class that inherits. This book uses superclass/subclass throughout for consistency.
| Class inherited from | Class that inherits |
|---|---|
| superclass | subclass |
| base class | derived class |
| parent class | child class |
Given the Employee class, create a Developer class that inherits from Employee. The Developer class has one method, update_codebase(), which prints "Employee has updated the codebase". Then, use the Developer instance, python_dev, to call print_company() and update_codebase().
Interactive Code
- Answer
-
class Employee():
def print_company(self):
print("Employee works at Stark Industries")class Developer(Employee):
def update_codebase(self):
print("Employee has updated the codebase")python_dev = Developer()
python_dev.print_company()
python_dev.update_codebase()
Define three classes: Polygon, Rectangle, and Square:
Polygonhas the methodp_disp(), which prints"object is a Polygon".Rectangleinherits fromPolygonand has the methodr_disp(), which prints"object is a Rectangle".Squareinherits fromRectangleand has the methods_disp(), which prints"object is a Square".
Create an instance of each class. Then, for each instance, call all the methods the instance has access to.
Interactive Code
- Answer
-
class Polygon():
def p_disp(self):
print("object is a Polygon")class Rectangle(Polygon):
def r_disp(self):
print("object is a Rectangle")class Square(Rectangle):
def s_disp(self):
print("object is a Square")poly = Polygon()
rect = Rectangle()
sq = Square()poly.p_disp()rect.r_disp()
rect.p_disp()sq.s_disp()
sq.r_disp()
sq.p_disp()


