Hierarchical inheritance in Python lets you create a family tree of classes. It's like having a parent class that passes down traits to multiple child classes, each with their own unique features.
This powerful tool helps you organize and reuse code efficiently. You can build complex class structures, from general to specific, allowing for flexible and modular programming in object-oriented design.
Hierarchical Inheritance in Python
Key features of hierarchical inheritance
- Involves creating a base class (parent) and multiple derived classes (children)
- Base class contains common attributes and methods shared by derived classes (Animal class with eat() method)
- Derived classes inherit these attributes and methods (Cat and Dog classes inherit from Animal)
- Derived classes can have their own specific attributes and methods in addition to inherited ones
- Allows for specialization and customization of derived classes (Cat class with meow() method, Dog class with bark() method)
- Enables code reuse and creation of specialized classes
- Avoids duplication of common code in derived classes
- Derived classes can override or extend inherited methods as needed (Dog class overrides eat() method)
isinstance()
function checks if an object is an instance of a specific class or any of its parent classes- Helps determine the type and inheritance relationships of objects (
isinstance(cat, Animal)
returnsTrue
)
- Helps determine the type and inheritance relationships of objects (
Class structure design for inheritance
- Identify common attributes and behaviors shared by derived classes
- Define these common elements in the base class (Animal class with name attribute and eat() method)
- Determine specialized attributes and behaviors specific to each derived class
- Cat class with color attribute and meow() method
- Dog class with breed attribute and bark() method
- Create base class (superclass) with shared attributes and methods
class Animal:
withdef __init__(self, name):
anddef eat(self):
- Create each derived class (subclass), inheriting from base class using
class DerivedClass(BaseClass):
syntaxclass Cat(Animal):
andclass Dog(Animal):
- Add specific attributes and methods to each derived class as needed
def __init__(self, name, color):
anddef meow(self):
in Cat classdef __init__(self, name, breed):
anddef bark(self):
in Dog class
Multilevel inheritance for tree-like organization
- Involves creating a hierarchy of classes with multiple levels
- Base class (Animal) serves as parent for one or more derived classes (Mammal)
- Derived classes can serve as parent classes for further derived classes (Cat and Dog inherit from Mammal)
- Each class in hierarchy inherits attributes and methods from its parent class
- Allows for creation of tree-like structure of classes (Animal โ Mammal โ Cat/Dog)
- When accessing attributes or methods, Python searches in current class, then parent class, and continues up hierarchy until found or top reached (method resolution order)
- Cat object can access eat() method defined in Animal class
- Enables creation of increasingly specialized classes
- Each level in hierarchy adds more specific attributes and behaviors (Mammal class adds nurse() method, Cat class adds meow() method)
Object-Oriented Programming Concepts in Hierarchical Inheritance
- Inheritance: Allows classes to inherit attributes and methods from other classes
- Encapsulation: Bundles data and methods that operate on that data within a single unit or object
- Subclass: A class that inherits from another class, also known as a derived class
- Superclass: A class from which other classes inherit, also known as a base class