Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo
Introduction to Python Programming

13.1 Inheritance basics

Introduction to Python Programming13.1 Inheritance basics

Learning objectives

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.

Checkpoint

is-a relationship between Employee and Developer

Concepts in Practice

Relationships between classes

1.
What is the relationship between a Doughnut class and a Pastry class?
  1. is-a
  2. has-a
2.
What is the relationship between a Kitchen class and a Freezer class?
  1. is-a
  2. has-a
3.
A goalkeeper is a player. Goalkeeper is a ______class. Player is a ______class.
  1. super; sub
  2. sub; super

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

Checkpoint

Using inheritance to create subclasses

Concepts in Practice

Creating subclasses

4.
How is a Daisy class that inherits from the Plant class defined?
  1. class Plant(Daisy):
  2. class Daisy(Plant):
  3. class Daisy:
    class Plant:
    
5.
Suppose a CarryOn class is inherited from a Luggage class. How is a CarryOn instance created?
  1. small_bag = CarryOn()
  2. small_bag = Luggage(CarryOn)
  3. small_bag = CarryOn(Luggage)
6.
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')
  1. func_1() only
  2. func_2() only
  3. func_1() and func_2()
7.
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')
  1. func_1() only
  2. func_2() only
  3. func_1() and func_2()

Alternative inheritance terms

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
Table 13.1

Try It

Employee and Developer classes

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().

Try It

Polygon classes

Define three classes: Polygon, Rectangle, and Square:

  • Polygon has the method p_disp(), which prints "object is a Polygon".
  • Rectangle inherits from Polygon and has the method r_disp(), which prints "object is a Rectangle".
  • Square inherits from Rectangle and has the method s_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.

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

© Jul 30, 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.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.