Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical example of Polymorphism

Can anyone please give me a real life, practical example of Polymorphism? My professor tells me the same old story I have heard always about the + operator. a+b = c and 2+2 = 4, so this is polymorphism. I really can't associate myself with such a definition, since I have read and re-read this in many books.

What I need is a real world example with code, something that I can truly associate with.

For example, here is a small example, just in case you want to extend it.

>>> class Person(object):     def __init__(self, name):         self.name = name  >>> class Student(Person):     def __init__(self, name, age):         super(Student, self).__init__(name)         self.age = age 
like image 954
Maxx Avatar asked Sep 16 '10 06:09

Maxx


People also ask

What are real-life examples of polymorphism?

Real-life Illustration: PolymorphismLike a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.

What is polymorphism in oops with example?

Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method sound() . Since this is a generic class so we can't give it a implementation like: Roar, Meow, Oink etc.

Where is polymorphism used in real time?

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.

What is the real time example of polymorphism in Java?

2. The best example of polymorphism is human behavior. One person can have different behavior. For example, a person acts as an employee in the office, a customer in the shopping mall, a passenger in bus/train, a student in school, and a son at home.


1 Answers

Check the Wikipedia example: it is very helpful at a high level:

class Animal:     def __init__(self, name):    # Constructor of the class         self.name = name     def talk(self):              # Abstract method, defined by convention only         raise NotImplementedError("Subclass must implement abstract method")  class Cat(Animal):     def talk(self):         return 'Meow!'  class Dog(Animal):     def talk(self):         return 'Woof! Woof!'  animals = [Cat('Missy'),            Cat('Mr. Mistoffelees'),            Dog('Lassie')]  for animal in animals:     print animal.name + ': ' + animal.talk()  # prints the following: # # Missy: Meow! # Mr. Mistoffelees: Meow! # Lassie: Woof! Woof! 

Notice the following: all animals "talk", but they talk differently. The "talk" behaviour is thus polymorphic in the sense that it is realized differently depending on the animal. So, the abstract "animal" concept does not actually "talk", but specific animals (like dogs and cats) have a concrete implementation of the action "talk".

Similarly, the "add" operation is defined in many mathematical entities, but in particular cases you "add" according to specific rules: 1+1 = 2, but (1+2i)+(2-9i)=(3-7i).

Polymorphic behaviour allows you to specify common methods in an "abstract" level, and implement them in particular instances.

For your example:

class Person(object):     def pay_bill(self):         raise NotImplementedError  class Millionare(Person):     def pay_bill(self):         print "Here you go! Keep the change!"  class GradStudent(Person):     def pay_bill(self):         print "Can I owe you ten bucks or do the dishes?" 

You see, millionares and grad students are both persons. But when it comes to paying a bill, their specific "pay the bill" action is different.

like image 84
Escualo Avatar answered Oct 01 '22 14:10

Escualo