Inheritance basics

13.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.