This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Inheritance

The four principles of OOP can be summarised with the acronym APIE:

  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation

We will explore these principles (but in a different order).

Inheritance

We will first look at one of the most important pillars of OOP, that is inheritance.

I have shown you an example of inheritance in my video: a Character is the base class, which is inherited by Hero and Enemy. In turn, Knight and Magician inherit the properties of Hero, while Monster and BigBoss inherit them from Enemy.

Inheritance example

The idea behind inheritance is that you will not need to modify existing code just so that you can add some new functionality. Otherwise, this will result in a lot of duplicated code.

Inheritance represents a is-a relation.

  • Mammal is a type of animal
  • Programming Course is a type of course.

When you inherit from a class, you inherit features from the class and add new features to it. This allows for the code to be reused, obeying the Don’t Repeat Yourself (DRY) principle of Software Engineering.

If A inherits from B, then A is called the subclass and B is called the superclass. In our example:

  • Character is the superclass of Hero and Enemy.
  • Knight is a subclass of Hero

All classes in Python 3 are subclasses of object.

class Person:
    pass

p = Person()
issubclass(Person, object)  
isinstance(p, object)