Class Inheritance is a core feature of Object-Oriented Programming. It allows one class to inherit data or behavior from another class and is one of the key ways in which reuse is enabled within classes.

What is Inheritance?

Inheritance allows features defined in one class to be inherited and reused in the definition of another class.

Syntax of Class Inheritance in Python

A class that is defined as extending a parent class has the following syntax:

class BaseClassName:
    class-body

class SubClassName(BaseClassName):
    class-body

Note that the parent class is specified by providing the name of that class in round brackets after the name of the new (child) class.

Example of Class Inheritance

class Account:   

    def __init__(self, account_number, account_holder_name):
        self.account_number = account_number
        self.account_holder_name = account_holder_name
        print('Initialized Account class')

    def rate_of_interest(self):
        roi = 5
        print("Rate of Interest for this account - {} is {}".format(self.account_number, roi))
        return roi


class SavingsAccount(Account):

    def __init__(self, account_number, account_holder_name, account_type):
        super().__init__(account_number, account_holder_name)
        self.account_type = account_type

    def type_of_account(self):
        print("This is a Savings Account Type")
        return "savings"

In an object-oriented system we can achieve the reuse of data or behavior via inheritance. That is one class (in this case the SavingsAccountclass) can inherit features from another class (in this case Account).

class inheritance in python

In above example we do several things:

  1. The class is called SavingsAccountbut it extends Account. This is indicated by including the name of the class being inherited in parentheses after the name of the class being defined (e.g. SavingsAccount(Account)) in the class declaration.
  2. Inside the __init__ method of SavingsAccount class we reference the __init__() method of the class Account . This allows whatever initialization is required for Account to happen. As this is called from within the SavingsAccount class’s __init__() which then allows any initialization required by the SavingsAccount to occur.
  3. Note that the call to the super().__init__() initializer can come anywhere within the SavingsAccount.__init__() method; but by convention it comes first to ensure that whatever the Accountclass does during initialization does not over write what happens in the SavingsAccount class.
  4. All instances of the class Account have a account_number, and account_holder_nameand have the behaviour rate_of_interest().
  5. All instances of the class SavingsAccount have account_numberaccount_holder_nameand account_type as attributes and have the behaviors rate_of_interest() and type_of_account().
  6. The method type_of_account()defined in the SavingsAccount class can access the attributesaccount_numberaccount_holder_name just as it can access the attribute account_type.

Now let’s execute below code snippet,

acct = Account('111111111', 'AccountOne')
acct.rate_of_interest()

print('------------------------------------------')

savings_acct = SavingsAccount('222222222', 'AccountTwo', 'savings')
savings_acct.rate_of_interest()
savings_acct.type_of_account()

Above code snippet will give us output as below,

Initialized Account class
Rate of Interest for this account - 111111111 is 5
------------------------------------------
Initialized Account class
Rate of Interest for this account - 222222222 is 5
This is a Savings Account Type

Conclusion

To recap on the concept of inheritance. Inheritance is supported between classes in Python. For example, a class can extend (subclass) another class or a set of classes.

A subclass inherits all the methods and attributes defined for the parent class(es) but may override these in the subclass.

A subclass,

  • inherits from a super class
  • obtains all code and attributes from the super class
  • can add new code and attributes
  • can override inherited code and attributes
  • can invoke inherited behaviour or access inherited attributes