In this Python article we want to learn about Hierarchical Inheritance in Python, so in python object oriented programming When more than one derived classes are created from a single base class that is called hierarchical inheritance.
Now let’s take a look at this image.
If you see in the image, we have four classes, Class A is our base or parent class, we have another three classes and all of them are extending from just one base class, as i have already said When more than one derived classes are created from a single base class that is called hierarchical inheritance. now there is a relationship between Class A and three derived classes(B, C, D), but there is no relationship between the derived classes, it means that we have a relationship between base class and derived classes, but there is no relationship between derived classes, for example we don’t have any relationship between Class B, Class C and Class D.
Now let’s create practical example in Python Hierarchical Inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Animal: def run(self, name): self.name = name print(f'{self.name} Is Runing') class Dog(Animal): pass class Cat(Animal): pass dog = Dog() dog.run("Dog") cat = Cat() cat.run("Cat") |
Run the code and this is the result.
FAQs:
What is the hierarchy of inheritance in Python?
In Python hierarchy of inheritance refers to the order in which classes inherit attributes and methods from their parent classes. The hierarchy typically follows a tree-like structure, where subclasses inherit from superclasses, and it forms parent-child relationships.
What is hierarchical inheritance with example?
Hierarchical inheritance in Python occurs when a single parent class has multiple subclasses. Each subclass inherits attributes and methods from the same parent class but may also have its own unique attributes and methods. For example, consider a parent class Animal with subclasses Dog, Cat, and Bird. All these subclasses inherit common attributes and methods from the Animal class, such as eat() and sleep(), but may also have their own specific attributes and methods.
What is hierarchy in Python?
In Python, hierarchy refers to the arrangement of elements or entities in a structured order, and it will create relationships between them. This can include hierarchical relationships between classes in object-oriented programming, where subclasses inherit from superclasses, forming a hierarchy of inheritance.
What are the four types of inheritance in Python?
Python supports four types of inheritance:
- Single inheritance: When a class inherits from only one parent class.
- Multiple inheritance: When a class inherits from multiple parent classes.
- Multilevel inheritance: When a class inherits from a parent class, and another class inherits from this derived class, forming a chain of inheritance.
- Hierarchical inheritance: When a single parent class has multiple subclasses that inherit from it.
Learn More:
Subscribe and Get Free Video Courses & Articles in your Email