(C) POLYMORPHISM in Object Oriented Software Design
==========================================================
(1) The word polymorphism means the occurrence of different forms, stages, or types.
(2) In the context of object-oriented programming (OOP), polymorphism is the ability to create a variable, a function, or an object that has more than one form. "Poly" means many, and "morphism" means form.
(3) As an example, the function "add(x, y)" can take many different forms depending on the types of argument passed to it. Most of the time the many forms behave "in different manners" but "in similar manners".
(4) Assume that we have four(4) input types below for arguments x and y. In programming, arguments are also called parameters. Assume also that we are using the python programming language.
Input data types:
(4a) x and y can be integers
(4b) x and y can be real numbers (decimals)
(4c) x and y can be strings
(4d) x and y can be lists of numbers, arrays of numbers, list of strings, or mixed arrays, etc.
Depending on the input data types above, the function for add(x, y) takes different forms as shown below.
# FOR INTEGER AND REAL NUMBERS
def add(x, y):
return (x+y)
# FOR STRINGS
def add(x, y):
return ("x" + "y")
# FOR LISTS (PYTHON ARRAYS)
def add(x, y):
return (x.append(y))
# FOR NUMPY ARRAYS (WITH RULES ON INPUTS)
def add(x, y):
return (x + y)
EXPECTED RESULTS OF add(x, y) EXECUTIONS
=================================================
Integers: x = 3 and y = 8
add(x, y) = 11
Real numbers : x = 3.5 and y = 8.2
add(x, y) = 11.7
Strings: x = "Bismillah" and y = " from WRY"
add(x, y) = Bismillah from WRY
Python arrays: x = [1, 2, 4.5, 3.127, Hello] and y = [8, 8, 8, 8, "there"]
add(x, y) = [1, 2, 4.5, 3.127, Hello, 8, 8, 8, 8, "there"]
Numpy arrays: x and y must be numeric arrays of the same length. Otherwise illegal syntax.
def add(x, y):
return (x + y)
(6) Note that the concept of "polymorphism" is different from "inheritance with overiding". In polymorphism, the different forms is caused by the different "data types" whereas in overiding it is concerned with "inheritance". It means, for example, you inherit a particular behaviour and you need to change the behaviour for some important reason, possibly because of your nature of being "different objects", like between an "automobile.drive(speed)" and an "aircraft.drive(speed)" as we explained in Part 3B. The data types for speed is the same (real numbers) in both cases. The actions for driving an automobile are different from those for an aircraft.
(7) Polymorphism is not about inheritance. Polymorphism is not caused by inheritance. Polymorphism is about the different forms of a particular function, and that it can exist at the same hierarchical level or different levels in the overall class structure. The need to behave differently in polymorphism is caused by the different data types. Because the same function is being used to do different things for different data types, these functions are called "overloaded functions" or "overloaded operators". It is just like a person who is a driver. He is also asked to do the gardening, the cooking, the washing, the house cleaning, etc, depending on the "commands from his employer". The person is essentially overloaded. So sometimes people say, polymorphism is the implementation of "overloading methods" in the class. Remember that in OOP, functions and procedures are also called "methods".
(8) So far we talked about the term "ad-hoc polymorphism" to refer to polymorphic functions which can be applied to arguments of different types, meaning functions that take many different forms depending on the types of argument passed to them. Polymorphism is "method overloading" and not "method overiding" as we discussed in Part 3B.
(9) There is another complex implementation of polymorphism in software, called "parametric polymorphism". This type of polymorphism is available in several object-oriented languages. It goes under the name "generics" for Ada or "templates" for Java and the C++ STL (Standard Template Library). The keyword here is "parametric" or parameters or arguments, meaning, again about the different data types passed to the function (method).
In our class BTSS project we will be using polymorphism.
===========================================================
POLYMORPHISM IN PYTHON FOR "+" OPERATOR OVERLOADING
===========================================================
wruslan@wruslan-ubuntu1004-rtai:~$ python
Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
FOR INTEGERS
>>> x = 3
>>> y = 8
>>> x + y
11
FOR REAL NUMBERS (DECIMALS)
>>> x = 3.5
>>> y = 8.2
>>> x + y
11.699999999999999
FOR PYTHON LISTS (ARRAYS) CAN HAVE MIXING WITH STRINGS
>>> x = [1, 2, 3]
>>> y = [4, 5, 6, 7, "Bismillah WRY"]
>>> x + y
[1, 2, 3, 4, 5, 6, 7, 'Bismillah WRY']
>>> y + x
[4, 5, 6, 7, 'Bismillah WRY', 1, 2, 3]
FOR NUMPY ARRAYS (NUMERICAL PYTHON) CANNOT HAVE MIXING WITH STRINGS
>>> import numpy
>>> from numpy import *
>>> x = numpy.array([1, 2, 3, 4, 5])
>>> y = numpy.array([6, 7, 8, 9, 10])
>>> x + y
array([ 7, 9, 11, 13, 15])
>>> y + x
array([ 7, 9, 11, 13, 15])
>>> x - y
array([-5, -5, -5, -5, -5])
>>> y - x
array([5, 5, 5, 5, 5])
>>>
We can implement the above easily in the class methods.
===========================================================
--
WASSALAM
wruslan.hahaha
Microsoft Word Version
Return to Software Engineering Fundamentals (SEF9MMUWRY)
Previous Topic: 41 Inheritance in Object Oriented (OO) software design
Next Topic: 43 Encapsulation in Object Oriented (OO) software design
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment