13.1 Inheritance basics

2 min readjune 24, 2024

Object-oriented design relationships are crucial in Python programming. They help organize code and create efficient, reusable structures. Understanding is-a and has-a relationships, along with and hierarchies, is key to building robust object-oriented systems.

Implementing allows for code reuse and specialization. By defining parent and child classes, programmers can create complex structures that mirror real-world relationships. This approach enhances code organization and promotes modularity in software development.

Object-Oriented Design Relationships

Is-a vs has-a relationships

  • Is-a relationship represents inheritance between classes indicates one is a specialized version of another (Car is a Vehicle)
  • Has-a relationship represents composition or aggregation between classes indicates one contains or is composed of another (Car has an Engine)

Superclass and subclass hierarchy

  • Superclass (parent class or ) more general class contains common attributes and methods provides blueprint for subclasses to inherit from can be inherited by multiple subclasses
  • Subclass (child class or ) more specialized class inherits attributes and methods from superclass can add new attributes and methods specific to subclass can override or extend functionality of inherited methods
  • Inheritance hierarchy represents relationship between superclasses and subclasses subclasses inherit from superclasses creating hierarchical structure allows code reuse and creation of specialized classes based on more general ones

Implementation of inheritance

  • Defining parent class
    1. Create class containing common attributes and methods
    2. Use
      class
      keyword followed by class name
    3. Example:
      class Vehicle:
          def [__init__](https://www.fiveableKeyTerm:__init__)([self](https://www.fiveableKeyTerm:self), make, model):
              self.make = make
              self.model = model
          
          def start_engine(self):
              print("Starting the engine")
      
  • Defining child class
    1. Create class inheriting from parent class
    2. Use
      class
      keyword followed by class name and parent class name in parentheses
    3. Example:
      class Car(Vehicle):
          def __init__(self, make, model, num_doors):
              [super()](https://www.fiveableKeyTerm:super()).__init__(make, model)
              self.num_doors = num_doors
          
          def honk(self):
              print("Honk honk!")
      
  • Using parent and child classes
    • Create instances of child class which inherit attributes and methods from parent class
    • Access inherited attributes and methods using dot notation
    • Example:
      my_car = Car("Toyota", "Camry", 4)
      my_car.start_engine()  # Inherited from Vehicle
      my_car.honk()  # Specific to Car
      

Object-Oriented Programming Concepts

  • is a programming paradigm that organizes code into objects, which are instances of classes
  • : Simplifying complex systems by modeling classes based on essential properties and behaviors
  • : Bundling data and methods that operate on that data within a single unit or object
  • : The order in which Python searches for methods in a class hierarchy during inheritance

Key Terms to Review (27)

__bases__: __bases__ is a term used in the context of object-oriented programming, specifically in the discussion of inheritance and multiple inheritance. It refers to the parent or superclass(es) from which a derived or subclass inherits properties and methods.
__init__: __init__ is a special method in Python that is automatically called when an object of a class is created. It is used to initialize the attributes of the object, setting them to their starting values.
__str__: The __str__ method is a special method in Python that is used to provide a string representation of an object. It is called when an object is converted to a string, such as when using the print() function or the str() built-in function.
__subclasses__: __subclasses__ is a built-in function in Python that returns a list of all the immediate subclasses of a given class. This function is particularly useful in the context of inheritance, as it allows you to easily identify and work with the classes that inherit from a specific base class.
Abstraction: Abstraction is the process of simplifying complex systems or entities by focusing on their essential features and hiding unnecessary details. It allows programmers to manage complexity and create more modular, reusable, and maintainable code.
Base Class: A base class, also known as a superclass, is the fundamental class from which other classes inherit properties and methods. It serves as the foundation for creating more specialized classes, allowing for the reuse of common functionality and the establishment of hierarchical relationships between classes.
Class: A class is a blueprint for creating objects, encapsulating data for the object and methods to manipulate that data. It allows for the creation of user-defined data structures.
Class: In the context of programming, a class is a blueprint or template that defines the structure and behavior of objects. It serves as a blueprint for creating objects, which are instances of the class, and encapsulates data and functionality within a cohesive unit.
Derived class: A derived class is a type of class in object-oriented programming that inherits properties and behaviors (methods) from another class, known as the base class. This allows the derived class to extend or modify the functionalities of the base class while retaining its core features. The relationship between derived and base classes forms the foundation for various inheritance structures, enabling code reuse and more organized program design.
Encapsulation: Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling data and methods into a single unit, known as a class. It is the mechanism that allows objects to hide their internal implementation details from the outside world, providing a well-defined interface for interacting with the object.
Hierarchical inheritance: Hierarchical inheritance is a type of inheritance in object-oriented programming where multiple derived classes inherit from a single base class. This allows for shared functionality and attributes to be defined in the base class and reused across multiple subclasses.
Inheritance: Inheritance is a fundamental concept in object-oriented programming where a new class is created based on an existing class, inheriting its attributes and behaviors. This allows for code reuse, hierarchical organization, and the creation of specialized classes that build upon the foundation of more general ones.
Isinstance(): The isinstance() function in Python is used to check the type of an object. It determines whether an object is an instance of a specified class or any of its subclasses. This function is particularly useful when working with variables, type conversion, mixed data types, boolean values, inheritance, and hierarchical inheritance.
Issubclass(): The issubclass() function in Python is used to check if a class is a subclass of another class. It determines the inheritance relationship between two classes, allowing you to assess if one class is derived from or inherits from another class.
Method Overriding: Method overriding is a feature in object-oriented programming where a subclass provides its own implementation of a method that is already defined in its superclass. This allows the subclass to customize or override the behavior of the inherited method to better suit its specific needs.
Method Resolution Order: Method resolution order (MRO) is a concept in object-oriented programming that determines the order in which a class searches for a method to be executed when it is called on an object. It is a crucial aspect of understanding how inheritance and multiple inheritance work in programming languages like Python.
Multiple inheritance: Multiple inheritance allows a class to inherit attributes and methods from more than one parent class. This can lead to complex hierarchies but provides a way to integrate diverse functionalities.
Multiple Inheritance: Multiple inheritance is a feature in object-oriented programming where a class can inherit properties and methods from more than one parent class. This allows the creation of complex class hierarchies and the reuse of code across different class structures.
Object-Oriented Programming: Object-Oriented Programming (OOP) is a programming paradigm that focuses on creating objects, which are instances of classes, to represent and manipulate data. It emphasizes the use of encapsulation, inheritance, and polymorphism to create modular, reusable, and maintainable code.
Pass: The 'pass' statement in Python is a placeholder that does nothing. It is used as a way to indicate that a section of code should do nothing, often used as a temporary measure or when a statement is required syntactically but no action is needed. It is a way to create an empty block of code, allowing the program to continue executing without errors.
Pass-by-object-reference: Pass-by-object-reference is a method of passing arguments to functions where the reference (or address) of the object is passed, not the actual object itself. This means changes made to the object within the function affect the original object outside the function.
Polymorphism: Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables objects to take on multiple forms, allowing them to respond to the same method call in different ways.
Self: The 'self' in programming refers to the instance of an object or class that the current method or function is operating on. It is a way for an object to refer to and access its own attributes and methods.
Single Inheritance: Single inheritance is a fundamental concept in object-oriented programming where a class inherits properties and methods from a single parent class. This inheritance relationship establishes a hierarchical structure, allowing the child class to access and utilize the attributes and behaviors defined in the parent class.
Subclass: A subclass is a class that inherits from another class, known as the superclass. Subclasses inherit the attributes and methods of the superclass, allowing them to reuse and extend the functionality of the parent class.
Super(): The 'super()' function is a built-in function in Python that allows a subclass to call a method or access an attribute from its parent or superclass. It is a crucial tool in the context of object-oriented programming, as it enables the reuse of code and the implementation of inheritance hierarchies.
Superclass: A superclass is the parent or base class in an inheritance hierarchy, from which one or more subclasses can inherit attributes and behaviors. It serves as the foundation for creating specialized classes that share common characteristics and functionality.
© 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