Types of inheritance PythonInheritance is the capacity of a particular class to obtain or inherit properties from another class and then use them when required. Inheritance has the following characteristics:
Example:Output: Jackie False johnny True Types of Inheritance in PythonThe types of inheritance depend on the number of children and parents involved. There are four kinds of inheritance available in Python: Single Inheritance Single inheritance allows a derivate class to inherit properties of one parent class, and this allows code reuse and the introduction of additional features in existing code. Example: Output: This function is defined inside the parent class. This function is defined inside the child class. Multiple Inheritance If a class is able to be created from multiple base classes, this kind of Inheritance is known as multiple Inheritance. When there is multiple Inheritance, each of the attributes that are present in the classes of the base has been passed on to the class that is derived from it. Example: Output: Father name is : Rajesh Mother name is : Shreya Multilevel inheritance, the features that are part of the original class, as well as the class that is derived from it, are passed on to the new class. It is similar to a relationship involving grandparents and children. Example: Output: John Jr Jr Grandfather name is : John Jr Jr Father name is : John Jr Son name is : John Hierarchical Inheritance If multiple derived classes are created from the same base, this kind of Inheritance is known as hierarchical inheritance. In this instance, we have two base classes as a parent (base) class as well as two children (derived) classes. Example: Output: This function is defined inside the parent class. This function is defined inside the child 1. This function is defined inside the parent class. This function is defined inside the child 2. Next TopicPython For Mechanical Engineers |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India