David J Nevin+= Code

Last updated: Jun 28, 2022

Inheritance

Parent Class

Parent Class (Super Class or Base Class): This class allows the reuse of its public properties in another class.

Child Class

Child Class (Sub Class or Derived Class): This class inherits or extends the superclass.

A child class has all public attributes of the parent class.

100
101
102
103
104
class ParentClass:
    # attributes of the parent class

class ChildClass(ParentClass):
    # attributes of the child class

Super() Function

We use super() as opposed to self to access the parent class properties instead of the child class properties of the same name.

Types of Inheritance

Single, Multi-level, Hierarchical, Multiple, and Hybrid

Single Inheritance

There is only one class extending from another class.

Multiple Inheritance

A class extends another class which itself extends another class.

Hierarchical

A base class is extended by distinct classes depending on the requirements.

Multiple Inheritance

A class extends or is derived from more than one base class.

100
101
102
class ClassName(BaseClass, AnotherBaseClass, ...):
    def method()
        pass

Advantages of Inheritance

Reusability, code modification, extensibility and data hiding.


Next: Terminal Tweaks
Previous: Information Hiding