13.3 Methods

3 min readjune 24, 2024

and are key concepts in object-oriented programming. They allow subclasses to customize inherited methods and enable objects of different classes to respond to the same method calls in unique ways.

These techniques promote code flexibility and reusability. By using the function and understanding different method types, programmers can create more efficient and adaptable class hierarchies in their Python projects.

Method Overriding and Polymorphism

Method overriding in subclasses

Top images from around the web for Method overriding in subclasses
Top images from around the web for Method overriding in subclasses
  • Allows subclasses to provide different implementations of methods already defined in parent classes
    • Overridden methods in subclasses must have same name, parameters, and return type as methods in parent classes ()
    • When objects call overridden methods, subclass implementations execute instead of implementations (Square and Circle classes overriding Area method from Shape class)
  • Useful when subclasses need to modify or extend behavior of inherited methods to suit specific requirements
  • To methods, define methods with same names in subclasses
    • [@override](https://www.fiveableKeyTerm:@override)
      decorator can indicate methods intended to override parent , but not mandatory in Python

Super function for parent methods

  • super()
    function allows subclasses to call methods from parent classes, even if methods are overridden in subclasses
    • Returns temporary objects of parent classes, allowing access to methods and attributes
  • Calling
    [super().method_name()](https://www.fiveableKeyTerm:super().method_name())
    within overridden methods in subclasses invokes parent class implementations of methods
    • Useful when subclasses want to extend behavior of parent class methods rather than completely replacing them (Square class calling
      super().Area()
      to include additional calculations)
  • super()
    function can also call parent class constructors (
    [__init__](https://www.fiveableKeyTerm:__init__)
    methods) from subclass constructors
    • Ensures parent class initialization logic executes before subclass-specific initialization

Polymorphism in method calls

  • Ability of objects of different classes to respond to same method calls in different ways
  • Achieved through method overriding and in Python
    • Method overriding: subclasses provide different implementations of methods inherited from parent classes (Dog and Cat classes overriding Speak method from Animal class)
    • Method overloading: not directly supported in Python, but achievable through default arguments or using
      [*args](https://www.fiveableKeyTerm:*args)
      and
      [**kwargs](https://www.fiveableKeyTerm:**kwargs)
  • Allows for code reusability and flexibility
    • Single method calls can invoke different behaviors depending on object classes
    • Eliminates need for extensive conditional statements to handle different object types (Polymorphic MakeSound function calling Speak methods of Dog and Cat objects)
  • Promotes loose coupling between objects, as calling code doesn't need to know specific classes of objects it interacts with
    • As long as objects adhere to common interfaces (methods with same names and parameters), they can be used interchangeably

Types of Methods in Python

  • : Regular methods that operate on instance-specific data
  • : Methods that don't require access to instance-specific data, defined using
    @staticmethod
    decorator
  • Class methods: Methods that operate on class-level data, defined using
    @classmethod
    decorator
  • : Technique where multiple method calls are chained together, with each method returning the object itself
  • : Defines the order in which Python searches for methods in class hierarchies, especially important in multiple inheritance scenarios

Key Terms to Review (18)

__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.
@override: @override is an annotation used in programming, particularly in object-oriented languages like Java and Python, to indicate that a method is intended to override a method declared in a superclass. This annotation serves as a way to ensure that the developer is correctly overriding a method, helping to prevent errors that can arise from misspelling the method name or using incorrect parameters.
**kwargs: **kwargs is a special syntax in Python used in function definitions to allow the passing of a variable number of keyword arguments. This enables functions to accept any number of additional arguments without requiring a specific parameter for each one, making code more flexible and easier to manage. When using **kwargs, the additional arguments are captured as a dictionary within the function, where the keys are the argument names and the values are the corresponding values passed in.
*args: *args is a special syntax in Python that allows a function to accept an arbitrary number of positional arguments. It is used to create more flexible and dynamic functions that can handle an unknown number of inputs.
Class Methods: Class methods are a type of method in object-oriented programming that are associated with the class itself, rather than with individual instances or objects of that class. They are defined within the class but operate on the class as a whole, rather than on specific instances.
Instance Methods: Instance methods are functions defined within a class that operate on the specific instances, or objects, created from that class. They provide a way for objects to interact with and manipulate their own data and behavior.
Method Chaining: Method chaining is a programming technique that allows you to call multiple methods on an object in a single line of code, with each method call returning the same object, enabling a fluent and expressive coding style.
Method Overloading: Method overloading is a feature in object-oriented programming that allows a class to have multiple methods with the same name, but with different parameters. The compiler selects the appropriate method to call based on the number, types, and order of the arguments passed at the time of the method call.
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 (MRO): Method Resolution Order (MRO) refers to the order in which a programming language searches for and executes methods within a class hierarchy. It determines the precedence of methods when multiple classes are involved, ensuring the correct method is called during runtime.
Method Signature: The method signature is a unique identifier for a method that defines its name, parameters, and return type. It is a crucial aspect of defining and using methods in programming languages like Python.
Override: Override allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is used to alter or extend the behavior of inherited methods.
Parent Class: The parent class, also known as the superclass, is the class from which other classes inherit properties and methods. It serves as the foundation for creating more specialized or derived classes, known as child or subclasses.
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.
Static Methods: Static methods are a type of method within a class in object-oriented programming that do not require an instance of the class to be called. They are associated with the class itself rather than any specific object of the class, and can be accessed directly using the class name.
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.
Super().method_name(): The 'super().method_name()' is a Python feature that allows a subclass to call a method defined in its superclass. It provides a way to access and utilize the functionality of the parent class within the subclass, enabling code reuse and inheritance.
© 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.