Python - Polymorphism
Why should I learn to solve Python: Polymorphism technical interview questions?
Learn and practise solving Python: Polymorphism technical interview questions and answers to enhance your skills for clearing technical interviews, HR interviews, campus interviews, and placement tests.
Where can I get technical Python: Polymorphism technical interview questions and answers with explanations?
IndiaBIX provides you with lots of fully solved Python: Polymorphism technical interview questions and answers with a short answer description. You can download Python: Polymorphism technical interview questions and answers as PDF files or e-books.
How do I answer Python: Polymorphism technical interview questions from various companies?
You can answer all kinds of Python: Polymorphism technical interview questions by practising the given exercises (short answer type). You can also find the frequently asked Python: Polymorphism technical interview questions with answers from various companies, such as TCS, Wipro, Infosys, CTS, IBM, etc.
Polymorphism in Python refers to the ability of a function or method to behave in different ways based on the type of object it is acting upon. There are two types of polymorphism in Python: compile-time polymorphism (also known as method overloading) and runtime polymorphism (also known as method overriding).
Let's take an example of runtime polymorphism with a simple program:
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
def animal_speak(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(animal_speak(dog))
print(animal_speak(cat))
Output:
Woof! Meow!
In this example, we have a base class Animal
with a method speak
. We then have two derived classes Dog
and Cat
, both of which override the speak
method to provide their own implementation.
The function animal_speak
takes an object of type Animal
as a parameter and calls its speak
method. Since the actual type of the object can be either Dog
or Cat
, this demonstrates runtime polymorphism.
Polymorphism in Python can be classified into two types: compile-time polymorphism (also known as method overloading) and runtime polymorphism (also known as method overriding).
Compile-Time Polymorphism:
Compile-time polymorphism occurs when the method or function to be executed is determined at compile time. This is achieved through method overloading, where multiple methods in the same class have the same name but different parameter lists.
class MathOperations:
def add(self, a, b):
return a + b
def add(self, a, b, c):
return a + b + c
# Example of Compile-Time Polymorphism
math_obj = MathOperations()
result1 = math_obj.add(2, 3)
result2 = math_obj.add(2, 3, 4)
print(result1)
print(result2)
Output:
TypeError: add() takes 3 positional arguments but 4 were given
In this example, attempting to call the add
method with different parameter counts will result in a TypeError
at compile time.
Runtime Polymorphism:
Runtime polymorphism occurs when the method to be executed is determined at runtime. This is achieved through method overriding, where a method in the child class has the same name and signature as a method in the parent class.
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Dog barks"
class Cat(Animal):
def speak(self):
return "Cat meows"
# Example of Runtime Polymorphism
def animal_speak(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(animal_speak(dog))
print(animal_speak(cat))
Output:
Dog barks Cat meows
In this example, the animal_speak
function can take different types of animals (objects of Dog
and Cat
), demonstrating runtime polymorphism.
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP), and it plays a crucial role in achieving one of the key principles of OOP: abstraction. Abstraction allows objects to be treated at a higher level, focusing on their behaviors and interactions rather than their internal details.
Polymorphism in OOP:
Polymorphism allows objects of different types to be treated as objects of a common base type. This enables code to work with objects at a higher level of abstraction, making the system more flexible and extensible. There are two main types of polymorphism in OOP: compile-time polymorphism (method overloading) and runtime polymorphism (method overriding).
Example Program:
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def speak(self):
return "Dog barks"
class Cat(Animal):
def speak(self):
return "Cat meows"
# Example of Polymorphism in OOP
def animal_speak(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(animal_speak(dog))
print(animal_speak(cat))
Output:
Dog barks Cat meows
In this example, animal_speak
is a function that takes any object of type Animal
or its subclasses. This demonstrates polymorphism, as the same function can work with different types of objects, treating them at a higher level of abstraction. The actual behavior is determined at runtime based on the specific type of object passed to the function.
Polymorphism, along with other OOP principles like encapsulation and inheritance, contributes to building modular, reusable, and maintainable code in complex software systems.
Method overloading in Python refers to the ability to define multiple methods in a class with the same name but different parameters. This allows a class to have multiple functions with the same name, but each function performs a different task based on the parameters provided.
Example Program:
class MathOperations:
def add(self, a, b):
return a + b
def add(self, a, b, c):
return a + b + c
# Creating an object of MathOperations
math_obj = MathOperations()
# Example of Method Overloading
result1 = math_obj.add(2, 3)
result2 = math_obj.add(2, 3, 4)
print(result1)
print(result2)
Output:
TypeError: add() takes 3 positional arguments but 4 were given
In this example, the MathOperations
class has two add
methods. The first add
method takes two parameters, and the second one takes three parameters. However, attempting to call the add
method with different parameter counts will result in a TypeError
at runtime.
It's important to note that Python does not support traditional method overloading like some other languages (e.g., Java). In Python, the method defined last with a particular name and signature will override any previous methods with the same name, and the overridden method will be called. Therefore, the example above would not work as intended in a true method overloading sense.
To achieve similar functionality in Python, you can use default values for parameters or use variable-length argument lists (e.g., *args, **kwargs).