Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Override method from parent class in Python

In object-oriented programming, method overriding is a concept that allows a subclass to provide a new implementation for a method that is already defined in its superclass. Here's a step-by-step tutorial on how to override methods in Python:

  • Define a base class with a method:

First, create a base class with a method you want to override in a subclass. In this example, we create a Vehicle class with a drive method:

class Vehicle:
    def drive(self):
        print("The vehicle is driving.")
  • Create a subclass that inherits from the base class:

Now, create a subclass that inherits from the base class. In this example, we create a Car subclass that inherits from the Vehicle class:

class Car(Vehicle):
    pass
  • Override the method in the subclass:

To override the drive method in the Car subclass, you need to define a new method with the same name in the subclass:

class Car(Vehicle):
    def drive(self):
        print("The car is driving.")

Now, when you call the drive method on a Car object, the new implementation in the Car subclass will be used instead of the one in the Vehicle base class.

  • Call the overridden method:

Create an object of the subclass and call the overridden method:

my_car = Car()
my_car.drive()  # Output: The car is driving.
  • Call the base class method using super():

Sometimes you might want to call the base class method from the subclass method. You can do this using the super() function:

class Car(Vehicle):
    def drive(self):
        print("Before driving the car:")
        super().drive()  # Call the base class method
        print("After driving the car.")

Now, when you call the drive method on a Car object, both the subclass and base class implementations will be called:

my_car = Car()
my_car.drive()
# Output:
# Before driving the car:
# The vehicle is driving.
# After driving the car.

This tutorial covered the basics of method overriding in Python, how to override methods in subclasses, and how to call the base class methods using the super() function. Understanding method overriding is important when working with inheritance, as it allows you to customize and extend the behavior of your classes.

  1. How to override methods in Python class:

    • Description: Method overriding in Python allows a subclass to provide a specific implementation for a method that is already defined in its superclass.
    • Code:
      class ParentClass:
          def some_method(self):
              print("Parent class method")
      
      class ChildClass(ParentClass):
          def some_method(self):
              print("Child class method")
      
      # Creating instances
      parent_obj = ParentClass()
      child_obj = ChildClass()
      
      # Method overriding
      parent_obj.some_method()  # Output: Parent class method
      child_obj.some_method()   # Output: Child class method
      
  2. Super() function and method overriding in Python:

    • Description: The super() function is used to call a method from the superclass in the context of the subclass, facilitating cooperative multiple inheritance.
    • Code:
      class ParentClass:
          def some_method(self):
              print("Parent class method")
      
      class ChildClass(ParentClass):
          def some_method(self):
              super().some_method()
              print("Child class method")
      
      # Creating an instance
      child_obj = ChildClass()
      
      # Method overriding with super()
      child_obj.some_method()
      
  3. Handling arguments and return values in overridden methods:

    • Description: Overridden methods can have different parameters and return values compared to the parent class, as long as the method signature is compatible.
    • Code:
      class ParentClass:
          def add(self, x, y):
              return x + y
      
      class ChildClass(ParentClass):
          def add(self, x, y, z):
              result = super().add(x, y)
              return result + z
      
      # Creating an instance
      child_obj = ChildClass()
      
      # Handling arguments and return values in overridden methods
      result = child_obj.add(1, 2, 3)
      print(result)  # Output: 6
      
  4. Multiple inheritance and method overriding in Python:

    • Description: Multiple inheritance allows a class to inherit from multiple superclasses, and method overriding works similarly as in single inheritance.
    • Code:
      class FirstClass:
          def some_method(self):
              print("First class method")
      
      class SecondClass:
          def some_method(self):
              print("Second class method")
      
      class ChildClass(FirstClass, SecondClass):
          pass
      
      # Creating an instance
      child_obj = ChildClass()
      
      # Method overriding in multiple inheritance
      child_obj.some_method()  # Output: First class method