7.3. Inheritance#

Inheritance is a fundamental concept in object orientated programming that allows new classes to inherit attributes and methods from an existing class. The existing class from which the new classes inherit is called the parent class (also known as base class or super class) and the new classes are called child class (also known as derived class and sub class).

The Python syntax for declaring a child class that inherits from a parent class is

class Parent_class:
    Parent class attributes and methods

class Child_class(Parent_class):
    Child class attributes and methods

Consider our Member class for members of the university which contains the attributes name and ID. Lets create a child class for students of the university. Enter the following code into your program.

# Inheritance
class Student(Member):
    pass


ellie = Student("Ellie Williams", 24123456)

print()
ellie.print()

Here we have declared a new class called Student that inherits from the Member class. The pass keyword does nothing and is a placeholder for future code. We then recreate the ellie object this time as an object of our new Student class and print the object details. Run your program and you should see the following added to the console.

University Member Details
-------------------------
Name     : Ellie Williams
ID       : 24123456

Note that the print() method from the parent class is inherited by the child class. We can confirm that the ellie object is a member of the Student class using the type() function. Enter the following code into your program.

print()
print(type(ellie))

Rerun your program and the following should be added to the console.

<class '__main__.Student'>

Thus confirming that ellie is a member of the Student class.

7.3.1. Child class attributes#

To add an attribute to a child class we simply declare it inside the class. Lets add the attribute position and set it equal to "student". Change your Student class so that it looks like the following.

class Student(Member):
    position = "Student"

To check that this attribute has been assigned to every object of the Student class enter the following code into your program.

print()
print(ellie.position)

Rerun your program and the following should be added to the console.

Student

7.3.2. Child class methods#

Methods for the child class are declared in the same way that we do for any other class. However, declaring a method in a child class with the same name as that of a method of the parent class will overwrite the parent class method. For example, the print() method in the Member class does not print the position attribute that we added to the Student class. To fix this enter the following code into your Student class.

def print(self):
    print("\nUniversity Member Details\n-------------------------")
    print(f"Name     : {self.name}")
    print(f"ID       : {self.ID}")
    print(f"Position : {self.position}")

Rerun your program and you should see the console output has changed to the following.

University Member Details
-------------------------
Name     : Ellie Williams
ID       : 24123456
Position : Student

7.3.3. The super() function#

The print() method that we added to the Student class repeats most if its code from the same method from its parent class Member. Instead of repeating this code we can use the super() function to give access to methods and properties of the parent class. Edit your print() method so that it looks like the following.

def print(self):
    super().print()
    print(f"Position : {self.position}")

Here we have used the super() function to access the print() function from the parent class Method and then added a line to print the position attribute from the child class Student. Rerun your program and you should see that the console output hasn’t changed.

7.3.4. Child class constructors#

We will probably want to use a different constructor for a child class. For example, lets add a constructor to the Student class that allows us to specify the course that the student is on. Add the following constructor to your Student class

def __init__(self, name, ID, course):
    super().__init__(name, ID)
    self.course = course

Here we have declared a constructor that takes in three inputs name, ID and course and have used the super() function to use the constructor from the parent class. We have also assigned the course attribute to the input. To use this updated class, change your ellie object declaration so that it looks like the following.

ellie = Student("Ellie Williams", 24123456, "Mathematics")

Finally, add the following line of code to the print() method to print the course attribute.

print(f"Course   : {self.course}")

Run your program and you should see the console output changed to the following.

University Member Details
-------------------------
Name     : Ellie Williams
ID       : 24123456
Position : Student
Course   : Mathematics

7.3.5. Exercises#

Exercise 7.5

Declare the following classes that inherits from the Vehicle class from Exercise 7.3:

  1. A Car class with a constructor that sets an attribute number_of_wheels to 4

  2. A Bicycle class that sets an attribute number_of_wheels to 2

For each child class add a method called print() that prints the details of the object. Use your new classes to declare the following objects and print their details:

  • A Car object called chitty that has a name Chitty Chitty Bang Bang and a maximum speed of 50 mph

  • A Bicycle object called bmx that has a name BMX and a maximum speed of 20 mph

Exercise 7.6

Declare the following classes that inherit from the Shape class from Exercise 7.4:

  1. A Triangle class that has three attributes side_1, side_2 and side_3 for the lengths of the sides of the triangle

  2. A Rectangle class that has two attributes width and height for the lengths of the sides of the rectangle

  3. A Circle class that has the attribute radius for the radius of the circle

For each of your child class declare constructors to calculate the area and circumference of the shape and print out details of the shape. Use your new classes to declare the following objects and print their details

  • A Triangle object that has side lengths 3, 4 and 5

  • A Rectangle object that has a width of 16 and a height of 9

  • A Circle object that has a radius of 5.

Hint: Heron’s formula may come in useful for calculating the area of a triangle.