MCS-201 · Unit 13

Classes in Python

Section 7 of 7

Block 4 · Unit 13Classes in Python11 min · Section 7 of 7

13.7Polymorphism

Covers IGNOU MCS-201 §13.7

Polymorphism (“many forms”) means that the same operation can work on objects of different types, each responding in its own way. It lets you write general code, such as “draw every shape” or “pay every employee”, without checking each object’s type.

The idea

In Python, polymorphism appears in three main ways:

FormMeaningExample
Duck typingAny object with the required methods can be used, whatever its classAny object with execute() can be passed to Animal.code()
Operator overloadingOne operator, different behaviour per type+ on numbers, lists, strings, Student
Method overridingA subclass redefines a parent methodSquare.area() replaces Shape.area()

Duck typing

“If it walks like a duck and quacks like a duck, it is a duck.” Python does not check an object’s class before calling a method; it simply calls the method. If the object has it, the call works.

Method overloading and overriding

Method overloadingMethod overriding
MeaningSeveral methods with the same name but different parameter lists in one classA subclass redefines a method inherited from its parent, with the same name
Supported in Python?No: a later def with the same name replaces the earlier oneYes
Python alternativeDefault arguments, *args, or checking argument typessuper().method() reaches the parent’s version
Decided at(In Java/C++: compile time)Run time, by the object’s class

Key takeaways

  • Polymorphism: one interface, many behaviours.
  • Duck typing: objects are used by their methods, not their class.
  • Overriding: a subclass redefines a parent method; super() reaches the original.
  • Python has no method overloading; use default arguments or *args.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is duck typing?

Show answer

Answer: B. Using an object because it has the needed methods, regardless of its class

Python simply calls the method; any object that provides it works.

Question 2 · multiple choice

What happens when a class defines two methods with the same name?

Show answer

Answer: D. The later definition replaces the earlier one

A def binds the name in the class namespace; the second binding overwrites the first.

Question 3 · multiple choice

What does this program print?

Python · quiz_poly.py
class Cat:
    def sound(self):
        return "Meow"

class Cow:
    def sound(self):
        return "Moo"

for a in [Cow(), Cat(), Cow()]:
    print(a.sound(), end=" ")
Show answer

Answer: C. Moo Meow Moo

Each object answers with its own sound() method; no common parent class is needed.

Question 4 · multiple choice

Which is an example of method overriding?

Show answer

Answer: B. A Square class redefining the area() method it inherits from Shape

Overriding replaces an inherited method in a subclass.

Question 5 · multiple choice

How can one Python method accept either two or three marks?

Show answer

Answer: C. By using a default parameter or *args, as in def total(self, *marks)

Default arguments and *args give one method several valid call forms.

Question 6 · short answer

Define polymorphism and explain its main benefit with an example from a payroll system.

Show worked answer

Polymorphism is the ability of one interface, such as a method name, to work with objects of different classes, each responding in its own way. In a payroll system, FullTimeEmployee, PartTimeEmployee and Contractor can each define pay() differently (monthly salary, hours × rate, invoice amount). The payroll code simply runs for e in employees: total += e.pay() without checking types. Adding a new kind of employee later only needs a new class with a pay() method; the payroll loop does not change.

↑ ↓ move↵ openTab switch type