0% found this document useful (0 votes)
3 views6 pages

OOPS Module 7

The document discusses polymorphism in Object-Oriented Programming (OOP), explaining its two main types: compile-time (static) and runtime (dynamic) polymorphism. It details method overloading and operator overloading as examples of compile-time polymorphism, while method overriding and duck typing are examples of runtime polymorphism. Additionally, it highlights Python's unique handling of these concepts, emphasizing its dynamic typing and lack of traditional method overloading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

OOPS Module 7

The document discusses polymorphism in Object-Oriented Programming (OOP), explaining its two main types: compile-time (static) and runtime (dynamic) polymorphism. It details method overloading and operator overloading as examples of compile-time polymorphism, while method overriding and duck typing are examples of runtime polymorphism. Additionally, it highlights Python's unique handling of these concepts, emphasizing its dynamic typing and lack of traditional method overloading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS SEVENTH MODULE

1. What is Polymorphism in OOPS?


Polymorphism is one of the important concepts of Object-Oriented Programming
(OOP). The word Polymorphism comes from Greek — Poly (many) + Morph
(forms), meaning. Polymorphism allows the same function or operation to behave
differently in different situations.

2. How many types of Polymorphism are there?

1. Compile-time Polymorphism (Static Polymorphism)


Occurs when the function to be called is decided at compile time.
Examples:
 Method Overloading (same method name, different parameters)
 Operator Overloading (like +, -, etc. in Python classes)
2. Runtime Polymorphism (Dynamic Polymorphism)
Occurs when the function call is resolved at runtime.
Examples:
 Method Overriding (child class overrides parent class method)
 Duck Typing (Python-specific dynamic polymorphism)

3. What is Compile Time Polymorphism?

In Python, compile-time polymorphism (also called static polymorphism) is not truly


supported the way it is in languages like Java or C++.
However, Python provides features that behave similarly. Compile-time polymorphism in
Python refers mainly to operator overloading, because Python does not support true method
overloading. Instead, it uses dynamic typing and allows only simulated overloading through
default or variable arguments.

Feature Python Support Purpose


❌ No (only Compile-time polymorphism in other
Method Overloading
simulated) languages
Operator
✔ Yes Python’s true static polymorphism
Overloading
Method Overriding ✔ Yes Runtime polymorphism

4. What is Run Time Polymorphism?

Runtime Polymorphism (also called Dynamic Polymorphism or Method Overriding) is a


feature in OOP where the method to be executed is decided during runtime, not at compile
time. When a child class provides its own version of a method that already exists in the parent
class, and which method gets called is decided at runtime → this is runtime polymorphism.
5. What is Method overloading in Python?

In Python, method overloading (like in Java/C++) does NOT exist in the traditional sense
because Python does not allow multiple methods with the same name but different
parameters. However, Python supports method overloading behaviour in other ways.

Prompt for Method Overloading with arguments:

class Calculator:
def add(self, *nums):
print("Sum =", sum(nums))

c = Calculator()
[Link](5)
[Link](5, 10)
[Link](5, 10, 15)

Output:

Sum = 5
Sum = 15
Sum = 30

Method overloading with arguments

6. What is Operator overloading in Python?

Operator Overloading in Python means *giving special meaning to operators (+, -, ==, >,
etc.) when they are used with user-defined objects. Python allows classes to override built-in
operator behaviour using special methods called magic methods or dunder methods (because
they start and end with __ __). Python built-in data types allow + operator can add numbers,
join strings or merge lists and * operator can be used to repeat instances of a string.
Prompt for Operator Overloading:

class A:
def __init__(self, a):
self.a = a
def __add__(self, o):
return self.a + o.a

ob1 = A(1)
ob2 = A(2)
ob3 = A("Sanchari")
ob4 = A(" Banerje")

print(ob1 + ob2) # integer addition


print(ob3 + ob4) # string concatenation

# actual working (internally)


print(A.__add__(ob1, ob2))
print(ob1.__add__(ob2))

Output:

3
Sanchari Banerjee
3
3

Operator Overloading (Compile time polymorphim)


7. What is Method Overriding in Python?

Method overriding is an ability of any object-oriented programming language that allows a


subclass or child class to provide a specific implementation of a method that is already
provided by one of its super-classes or parent classes. When a method in a subclass has the
same name, the same parameters or signature, and same return type (or sub-type) as a method
in its super-class, then the method in the subclass is said to override the method in the super-
class.

Prompt for Method Overriding:

# Python program to demonstrate


# Defining parent class
class Parent():

# Constructor
def __init__(self):
[Link] = "Inside Parent"

# Parent's show method


def show(self):
print([Link])

# Defining child class


class Child(Parent):

# Constructor
def __init__(self):
super().__init__() # Call parent constructor
[Link] = "Inside Child"

# Child's show method


def show(self):
print([Link])

# Driver's code
obj1 = Parent()
obj2 = Child()
[Link]() # Should print "Inside Parent"
[Link]() # Should print "Inside Child"

Output:
Inside Parent
Inside Child
Method Overriding using all kind of inheritance should be done by your
own.
7. What is Duck typing in Python?

Duck Typing is a concept in Python where the type of an object is less important than the
method or behaviour it provides. If an object behaves like what you need (has the required
method), Python will accept it — regardless of its actual class.

“If it looks like a duck, swims like a duck, and quacks like a duck,
then Python treats it as a duck.”

Key Idea
Python does not check:

 the class of an object


 the type of an object

It checks whether the object has the required method or attribute.

Prompt for Method Overriding:

class Duck:
def sound(self):
print("Quack Quack")

class Dog:
def sound(self):
print("Bark Bark")

def make_sound(obj):
[Link]() # only method name matters

d = Duck()
g = Dog()

make_sound(d)
make_sound(g)

Output:

Quack Quack
Bark Bark
Duck Typing (Run time polymorphim)

You might also like