This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< 7.3 Inheritance, Encapsulation and Polymorphism | Contents | CHAPTER 8. Complexity >


  1. OOP and POP are different. OOP has many benefits and is often more appropriate for use in large-scale projects.

  2. Class is the blueprint of the structure that allows us to group data and methods, while object is an instance from the class.

  3. The concept of “inheritance” is key to OOP, which allows us to refer attributes or methods from the superclass.

  4. The concept of “encapsulation” allows us to hide some of the private details of a class from other objects.

  5. The concept of “polymorphism” allows us to use common operation in different ways for different data input.


  1. Describe what are the differences of classes versus objects.

  2. Describe why we use self as the first argument in the methods.

  3. What is a constructor? And why do we use it?

  4. Describe the differences between class and instance attributes.

  5. The following is a definition of the class Point that takes in the coordinate x, y. Add a method plot_point that plot the position of point.

    import matplotlib.pyplot as plt
    class Point():
       def __init__(self, x, y):
           self.x = x
           self.y = y
  6. Use the class from problem 5, add a method calculate_dist which takes in x and y from another point, and returns the distance calculated between the two points.

  7. What’s inheritance?

  8. How do we inherit from a superclass and add new methods?

  9. When we inherit from a superclass, we need to replace a method with a new one, how do we do that?

  10. What’s the super method? Why do we need it?

  11. Create a class to model some real world object and create a new class to inherit from it. One example can be the following. You should use a different example and use as many things we learned as possible.

class Car():
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color
    def start_my_car(self):
        print('I am ready to drive!')
class Truck(Car):
    def __init__(self, brand, color, size):
        super().__init__(brand, color)
        self.size = size
    def start_my_car(self, key):
        if key == 'truck_key':
            print('I am ready to drive!')
            print('Key is not right')
    def stop_my_car(self, brake):
        if brake:
            print('The engine is stopped!')
            print('I am still running!')
truck1 = Truck('Toyota', 'Silver', 'Large')
truck1.stop_my_car(brake = False)
I am ready to drive!
I am still running!

< 7.3 Inheritance, Encapsulation and Polymorphism | Contents | CHAPTER 8. Complexity >