One of the major advantages of Object Oriented Programming is re-use. Inheritance is one of the mechanisms to achieve the same. In inheritance, a class (usually called superclass) is inherited by another class (usually called subclass). The subclass adds some attributes to superclass.
Below is a sample Python program to show how inheritance is implemented in Python.
python - Sample - python code :
python programming - Output :
wikitechy1 False
wikitechy2 True
How to check if a class is subclass of another?
Python provides a function issubclass() that directly tells us if a class is subclass of another class.
python - Sample - python code :
python programming - Output :
True
False
False
True
What is object class?
Like Java Object class, in Python (from version 3.x), object is root of all classes.
In Python 3.x, “class Test(object)” and “class Test” are same.
In Python 2.x, “class Test(object)” creates a class with object as parent (called new style class) and “class Test” creates old style class (without object parent).
Does Python support Multiple Inheritance?
Unlike Java and like C++, Python supports multiple inheritance. We specify all parent classes as comma separated list in bracket.
python - Sample - python code :
python programming - Output :
wikitechy1 True E101
How to access parent members in a subclass?
Using Parent class name
python - Sample - python code :
python programming - Output :
10, 20
Using super()
We can also access parent class members using super.
python - Sample - python code :
python programming - Output :
(10, 20)
Note that the above two methods are not exactly same. In the next article on inheritance, we will covering following topics.
1) How super works? How accessing a member through super and parent class name are different?
2) How Diamond problem is handled in Python?
Wikitechy tutorial site provides you all the learn python , python course for beginners , learn python online interactive , how to
learn python coding , best online python training , python online course certification free , python training for beginners , online
python course free , learn python online free course
Related Searches to OOPS in Python | Inheritance, examples of object, issubclass and super
python ooppython oop examplesoop examplespython oop bookobject in oopoop pythonexample of oopoop programming examplesinheritance oopoop inheritanceclass in oopsoop objectexamples of oop languagesobject oopoop classlearn python oopclass oopoops definitionpython oop for beginnersoop in pythondefine oopsinheritance in ooppython oop tutorialoops concepts in pythonexamples of object pronounspython object oriented programming exampleslearn object oriented programming pythonpython class superpython object oriented programmingobject oriented programming in pythonobject oriented programming pythonlearn python object oriented programmingobject oriented programming tutorialclass in object oriented programmingpython 3 object oriented programmingobject oriented programming inheritanceobject oriented programming examplesfeatures of object oriented programmingprinciples of object oriented programmingsuper pythonpython superadvantages of object oriented programmingclass object oriented programmingdefine object oriented programmingpython inheritancepython class inheritanceinheritance pythonpython inheritance exampleinheritance exampleclass inheritanceinheritance programmingexample of inheritanceinheritance in pythonpython 2 inheritanceclass inheritance python exampledefine inheritancemultiple inheritance