You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

13.4 Hierarchical inheritance

3 min readjune 24, 2024

in Python lets you create a family tree of classes. It's like having a 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 structures, from general to specific, allowing for flexible and modular programming in object-oriented design.

Hierarchical Inheritance in Python

Key features of hierarchical inheritance

Top images from around the web for Key features of hierarchical inheritance
Top images from around the web for Key features of hierarchical inheritance
  • Involves creating a (parent) and multiple derived classes (children)
    • Base contains common attributes and methods shared by derived classes ( class with method)
    • Derived classes inherit these attributes and methods ( and 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 method, Dog class with 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()](https://www.fiveableKeyTerm:isinstance())
    function checks if an object is an instance of a specific class or any of its parent classes
    • Helps determine the type and relationships of objects (
      isinstance(cat, Animal)
      returns
      True
      )

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 attribute and eat() method)
  • Determine specialized attributes and behaviors specific to each
    • Cat class with attribute and meow() method
    • Dog class with attribute and bark() method
  • Create base class () with shared attributes and methods
    • class Animal:
      with
      def [__init__](https://www.fiveableKeyTerm:__init__)(self, name):
      and
      def eat(self):
  • Create each derived class (), inheriting from base class using
    class DerivedClass(BaseClass):
    syntax
    • class Cat(Animal):
      and
      class Dog(Animal):
  • Add specific attributes and methods to each derived class as needed
    • def __init__(self, name, color):
      and
      def meow(self):
      in Cat class
    • def __init__(self, name, breed):
      and
      def 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 ()
    • 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 ()
    • 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 method, Cat class adds meow() method)

Object-Oriented Programming Concepts in Hierarchical Inheritance

  • Inheritance: Allows classes to inherit attributes and methods from other classes
  • : 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
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary