Example of Method Overriding:
class Shape:
def draw(self):
print("Drawing a generic shape")
class Circle(Shape):
def draw(self):
print("Drawing a circle")
class Square(Shape):
def draw(self):
print("Drawing a square")
class Triangle(Shape):
pass
# Creating instances of the classes
shape = Shape()
circle = Circle()
square = Square()
triangle = Triangle()
# Calling the draw method on each object
shape.draw() # Output: Drawing a generic shape
circle.draw() # Output: Drawing a circle
square.draw() # Output: Drawing a square
triangle.draw() # Output: Drawing a generic shape
Code language: Python (python)
In this example, we have a superclass Shape
with a method draw()
. We then define three subclasses: Circle
, Square
, and Triangle
, each of which inherits from the Shape
class.
The Circle
class overrides the draw()
method with its own implementation to print “Drawing a circle”.
The Square
class also overrides the draw()
method with its own implementation to print “Drawing a square”.
The Triangle
class does not provide its own draw()
method, so it inherits the method from the Shape
class.
When we create instances of these classes and call the draw()
method on each instance, the method from the subclass will be executed if it exists; otherwise, the method from the superclass will be called.
This demonstrates the concept of method overriding in Python, where subclasses can provide their own specific implementation for methods defined in the superclass. Method overriding allows you to achieve polymorphism and create specialized behavior for different subclasses while maintaining a common interface defined in the superclass.
How can you override a method in Python?
Example to demonstrate how you can override a method in Python:
class Vehicle:
def start_engine(self):
print("Generic vehicle engine starting")
class Car(Vehicle):
def start_engine(self):
print("Car engine starting... Vroom!")
class Motorcycle(Vehicle):
def start_engine(self):
print("Motorcycle engine starting... Vroom! Vroom!")
class Bicycle(Vehicle):
pass
# Creating instances of the classes
vehicle = Vehicle()
car = Car()
motorcycle = Motorcycle()
bicycle = Bicycle()
# Calling the start_engine method on each object
vehicle.start_engine() # Output: Generic vehicle engine starting
car.start_engine() # Output: Car engine starting... Vroom!
motorcycle.start_engine() # Output: Motorcycle engine starting... Vroom! Vroom!
bicycle.start_engine() # Output: Generic vehicle engine starting
Code language: Python (python)
In this example, we have a superclass Vehicle
with a method start_engine()
. We then define three subclasses: Car
, Motorcycle
, and Bicycle
, each of which inherits from the Vehicle
class.
The Car
class overrides the start_engine()
method with its own implementation to print “Car engine starting… Vroom!”.
The Motorcycle
class also overrides the start_engine()
method with its own implementation to print “Motorcycle engine starting… Vroom! Vroom!”.
The Bicycle
class does not provide its own start_engine()
method, so it inherits the method from the Vehicle
class.
When we create instances of these classes and call the start_engine()
method on each instance, the method from the subclass will be executed if it exists; otherwise, the method from the superclass will be called.
This demonstrates how method overriding in Python allows subclasses to provide their own specific implementation for methods defined in the superclass, enabling different behaviors for different types of objects.
What is method overloading and method overriding in Python with example?
- Method Overloading:
Method overloading, in Python, is the ability to define multiple methods with the same name in a class, but with different numbers or types of parameters. Python does not support traditional method overloading like some other programming languages, but you can achieve similar behavior using default arguments or variable arguments.
Example of Method Overloading:
class Shape:
def area(self, x):
return x * x
def area(self, x, y):
return x * y
# Creating an instance of the class
shape = Shape()
# This will call the second area method due to method overloading in Python.
result1 = shape.area(5) # Output: Error - area() missing 1 required positional argument: 'y'
result2 = shape.area(5, 10) # Output: 50
Code language: Python (python)
In this example, we tried to create two area()
methods in the Shape
class with different numbers of parameters. However, Python does not natively support method overloading with the same method name, so the second method overrides the first one.
To achieve method overloading in Python, you can use default arguments or variable arguments.
- Method Overriding:
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The subclass uses the same method name and number of parameters as the one in the superclass, but it provides a different implementation to override the behavior of the superclass’s method.
Example of Method Overriding:
class Animal:
def make_sound(self):
print("Generic animal sound")
class Cat(Animal):
def make_sound(self):
print("Meow!")
# Creating an instance of the classes
animal = Animal()
cat = Cat()
animal.make_sound() # Output: Generic animal sound
cat.make_sound() # Output: Meow!
Code language: Python (python)
In this example, the Cat
class inherits from the Animal
class and overrides the make_sound()
method with its own implementation to print “Meow!” instead of the generic animal sound.
Method overriding allows you to provide specialized behavior in subclasses while maintaining a common interface defined in the superclass.
Remember that method overriding works based on the method name and number of parameters being the same in both the superclass and the subclass.
These examples illustrate the concepts of method overloading and method overriding in Python, demonstrating how they can be used to create flexible and polymorphic code in object-oriented programming.
Read More;
- How to use for loop in JSON Python?
- What is lambda function in Python for example?
- What is multilevel inheritance in Python with example?
- How do you use a any () or a all () in Python?
- How to add Qt to Python With Example
- What are type hints in Python With Example?
- Python Script Example for DevOps
- What is enumerate() in Python with an example?
- What is a for else in Python With Example?
- What is the filter() function with an Example?
- What is module and example in Python?
- What is a dictionary in Python example?