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)
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:
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.")
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
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.
Create an object of the subclass and call the overridden method:
my_car = Car() my_car.drive() # Output: The car is driving.
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.
How to override methods in Python class:
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
Super() function and method overriding in Python:
super()
function is used to call a method from the superclass in the context of the subclass, facilitating cooperative multiple inheritance.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()
Handling arguments and return values in overridden methods:
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
Multiple inheritance and method overriding in Python:
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