Polymorphism
Polymorphism
The word ‘polymorphism’ is derived from a Greek word ‘polymorphous’ which means ‘many shaped’ or ‘having many forms’. In object-oriented programming ,polymorphism means that it is possible to ‘overload’ the symbols that we use in a program,so that the same symbol can have different meanings in different contexts.The symbol may be an operator or method(function) name.So both type of symbols can be overloaded which is ‘operator overloading’ and ‘function overloading’ respectively to give polymorphic behavior.So Polymorphism means that different objects can respond to the same message in different ,class specific ways.
The word ‘polymorphism’ is derived from a Greek word ‘polymorphous’ which means ‘many shaped’ or ‘having many forms’. In object-oriented programming ,polymorphism means that it is possible to ‘overload’ the symbols that we use in a program,so that the same symbol can have different meanings in different contexts.The symbol may be an operator or method(function) name.So both type of symbols can be overloaded which is ‘operator overloading’ and ‘function overloading’ respectively to give polymorphic behavior.So Polymorphism means that different objects can respond to the same message in different ,class specific ways.
Polymorphism may be classified as :
Adhoc - :Ad hoc which means for specific purpose only is applicable in both traditional and
object-oriented programming environments.
object-oriented programming environments.
Coercion :- The arithmetical operators which we are familiar with in all programming
languages exhibit a form of polymorphism known as ‘coercion’.Explicitly
converting one data type to another is known as ‘casting’. All type conversions are
called ‘coercion’. E.g int x=10; float y=7.5; double z=x*y;
Overloading a function by means of its parameter list means we can use the same
name for more than one function and differentiate them by means of different
parameter lists.
e.g
aFunction(float value_in)
aFunction(int value_in)
Universal polymorphism
Here a single message can be sent to a disparate set of objects and they will all be able to respond to it.
Here a single message can be sent to a disparate set of objects and they will all be able to respond to it.
Object –Oriented Polymorphism
| Add caption |
A) Method Polymorphism
1) Operator overloading -: It provides new meanings for existing operators to enable them to work with user defined types. They(Overloaded operators) are generally inherited by derived classes.
2) Inheritance polymorphism : This means the use of the same function name for methods in different classes which are all in same classification hierarchy.
3) Runtime polymorphism : This is inheritance polymorphism or operator overloading where
the type(s) of objects using the methods or operators are not known until run time. Here
‘dynamic binding’ is used.
the type(s) of objects using the methods or operators are not known until run time. Here
‘dynamic binding’ is used.
B) Polymorphism by parameter
1) In-class method overloading
This gives the ability to define a number of methods with the same name
in a single class, differentiated by the parameter lists.
e.g class demo{
1) In-class method overloading
This gives the ability to define a number of methods with the same name
in a single class, differentiated by the parameter lists.
e.g class demo{
Public:
aFunction(int value1)
aFunction(int value1,int value 2)
};
aFunction(int value1,int value 2)
};
2) Genericity
‘Genricity’ or ‘Parametric Polymorphism’ refers to the creation of methods
which are generic-applicable to a range of objects.A generic method will execute the
same implementation but it will be able to accept a range of types as parameters.This is
useful when implementing container classes.Genericity is achieved in C++ by the use of
templates.Templates allows us to build whole generic classes,so that the creation of
objects can depend on the types of parameters sent at run time.
‘Genricity’ or ‘Parametric Polymorphism’ refers to the creation of methods
which are generic-applicable to a range of objects.A generic method will execute the
same implementation but it will be able to accept a range of types as parameters.This is
useful when implementing container classes.Genericity is achieved in C++ by the use of
templates.Templates allows us to build whole generic classes,so that the creation of
objects can depend on the types of parameters sent at run time.
ENCAPSULATION – Encapsulation is the practice of including in an object everything it needs hidden from other objects in the system.The internal state of an object is not directly accessible from outside and cannot be altered by external changes to the application. Encapsulation is based on the linking together of an object’s state and behavior.