Python – Object Oriented Programming – Class Inheritance

Inheritance and Polymorphism

Instance and Class Data

Core Principles of OOP

  • Inheritance
    • Extending functionality of existing code
  • Polymorphism
    • Creating an unified interface
  • Encapsulation
    • Bundling of data and Methods

Instance-level data

  • name , salary are instance attributes
  • self binds to an instance

Class-level data

  • data shared among all instances of a class
  • define class attributes in the body of class
  • “Global variable” within the class –> all objects with this class will have this attribute

  • MIN_SALARY is shared among all instances
  • No use of the self to define a class attribute
  • use ClassName.ATTR_NAME to access the class attribute value


Why use class attributes ?

Global constants related to class

  • minimal or maximal values for attributes
  • commonl used values and constants  ( pi )

Class Methods

  • Methods are already shared for each instance of the same class
  • Class methods cannot use instance level data

Alternative constructors

  • can only have one __init__()
  • use class methods to create objects
  • use return to return an object
  • cls(...) will call __init__(...)




Exercise 2 / Alternative constructors


Class Inheritance

Code reuse

  1. Someone has already written / done the coding  –>
    1. Modules are great for fixed functionality
    2. OOP is great for customizing functionality
  2. DRY – Don’t Repeat yourself
    1. for example elements of a website –> click and view functionality


New class functionality = Old class functionality + extra


    • Bankaccount class has
      • attribute  : balance
      • method : withdraw()
    • Savingsaccount class
      • attribute : balance, interest_rate
      • method : withdraw(), compute_interest()
    • So both classe have a common attribute and method.
    • You can reuse the code for the attribute and the method that are in common by inheritance
    • If you have another class -> CheckingAccount
      • attribute: balance, limit
      • method : withdraw() and deposit()
      • you here too can reuse the balance and modified withdraw() – with a slight difference –

Implementing Class Inheritance

  • MyParent – Class whose functionality is being extended/inherited
  • MyChild – class that will inherit the functionality and add more

Child class has all of the parent data

Inheritance : “is-a” relationship

  • A SavingsAccount is a BankAccount        ( with special features )




Customizing functionality via inheritance

  • for example a SavingsAccount is a special kind of BankAccount that has all the bankaccount functionality, but has additional properties like’interest rate’ and a method to ‘compute interest’
  • What we have so far :
    •  we could create SavingsAccount, but we didn’t have any additional functionality (empty class)

Customizing constructors

  • Can run constructor of the parent class first by Parent.__init__(self, args ... ) 
  • You don’t have to call the parent constructor

Creating objects with a customized constructor

  • the new constructor when creating an instance of SavingsAccount will be called and a interest_rate will be initialized

Adding functionality

  • Add methods as usual
  • Can use the data from both the parent and the child class

  • the functionality / method –> compute interest will only available in the subclass SavingsAccount

Customizing functinality

  • what in case of a customized version of a method in the main class ( eg. witfraw in CheckingsAccount ith an additional parameter )

  • Can change the signature ( add parameters)
  • Use Parent.method(self, args … ) to call a mthod from parent class