The classes at top in hierarchy is typically described as the base class, parent class, or superclass, while class which extend base class is known as the subclass or child class. The relation between these two class base class ,derived classes or subclass is called "IS-A". Here is the class hierarchy of exception types in python.
|Hierarchy of exception types|
What inheritance brings in Object oriented programming ? - Re-usability of code. i.e : base class defines common functionality for derived class and allow derived class to focus on specific functionality related to that class.
In order to get holistic picture of inheritance, lets take an example of Numeric Progressions classes. Here Arithmetic progression and Geometric progressions are derived classes and Progression is base class. (Example reference : Data Structure and Algorithm by Goodrich)
The class hierarchy will looks like this :
|Hierarchy of progression classes|
Now we will be creating Arithmetic and Geometric implementation inheriting Progression:
In above classes defined . Progression inherit from object class, mother of all in python. Progression class defines constructor to declare current element and various method like incrementing sequence ,
getting next element and printing the sequence. ArithmaticProgression and GeometricProgression are inheriting Progression class and override advance method as per logic of ArithmaticProgression (incremented by constant value) and GeometricProgression (multiplied by constant value).
Sample output: ------------------------------------
>>> p = Progression(4) #start is initialized with 4
>>> p.print_progression(4) #for loop will iterate four times and execute next(self) of Progression
4 5 6 7
>>> a = ArithmeticProgression(increment=2, start=3) #Overwrite default value
>>> a.print_progression(4) #Overridden _advance() of ArithmeticProgression executed
3 5 7 9
>>> g =GeometricProgression( base=3, start=12) #Overwrite default value
>>> g.print_progression(3) # Overridden _advance() of GeometricProgression executed and each
# time values are multiplied by 3 using self._current *= self._base
12 36 108
- class Progression(object) vs class Progression/class Progression() : class Progression(object) is new style of writing class definition while class Progression: is old way of doing this.In order to unify type and class from python 2.2 new style of class definition was introduced. For detail refer this post.
- super(GeometricProgression,self).__init__(start) / How super() got changed in python 3 ?In python 2.x, for initializing member variables of parent class from derived class we need to pass parameters values via calling super(<current_class>, self).__init__(<arguments>). However, form python 3, <current_class>, self become optional. Refer this for detail.
Another way of accessing init of parent class by explicitly using class name. i.e : We have used Progression.__init__(self , start) , Progression is base class name.
Previous: Method overloading and Operator overloading