Header

Wednesday 4 September 2013

IGNOU BCA 4th sem Solved Assignment - What is polymorphism? Explain the advantages of polymorphism with an example.

What is polymorphism? Explain the advantages of polymorphism with an example.
Ans
 In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are several different kinds of polymorphism.
1) A variable with a given name may be allowed to have different forms and the program can determine which form of the variable to use at the time of execution. For example, a variable named USERID may be capable of being either an integer (whole number) or a string of characters (perhaps because the programmer wants to allow a user to enter a user ID as either an employee number - an integer - or with a name - a string of characters). By giving the program a way to distinguish which form is being handled in each case, either kind can be recognized and handled.
2) A named function can also vary depending on the parameters it is given. For example, if given a variable that is an integer, the function chosen would be to seek a match against a list of employee numbers; if the variable were a string, it would seek a match against a list of names. In either case, both functions would be known in the program by the same name. This type of polymorphism is sometimes known as overloading.
In C++, for example, the operator known as the plus sign (+) - which is effectively a simple named function - can be assigned to operate on two objects such that it adds them together (perhaps the most common form of the + operation) or, as in boolean searching, a + can indicate a logical "and" (meaning that both words separated by the + operator must be present in order for a citation to be returned). In another context, the + sign could mean an operation to concatenate the two objects or strings of letters on either side of the + sign.
A given operator can also be given yet another meaning when combined with another operator. For example, in the C++ language, a "++" following a variable can mean "increment this value by 1". The meaning of a particular operator is defined as part of a class definition. Since the programmer can create classes, the programmer can also define how operators work for this class of objects; in effect, the programmer can redefine the computing language.
3) Polymorphism can mean, as in the ML language, a data type of "any," such that when specified for a list, a list containing any data types can be processed by a function. (For example, if a function simply determines the length of a list, it doesn't matter what data types are in the list.)
4) In PHP, polymorphism means that if B is a descendant of A and a function can accept A as a parameter, it can also accept B.
The polymorphism concept is similar for all objected oriented languages the defination is same and function too. 
The polymorphism is defined below.  "one name multiple form"  The explaination is that the polymorphism is combination of two words  "poly" means many and "morphism " means form .the function overloading could be example for it.  void sum(int,int)  void sum(float,float)  void sum(double,int)  void sum(int,float)  in above example the function name is same but only signature is different.  and therefore this can be easily implemented in java. 

However, definite reasons to use polymorphism do arise. Most such reasons are notoriously hard to explain adequately in a few paragraphs -- and notoriously poorly explained in far too many programming books. Examples given in such books regrettably tend to be contrived to the point of utter uselessness -- yet this isn't wholly the books' fault.
A container (like a list or a vector) of pointers to objects is probably the most typical case for polymorphism. When one part of your code must register an object for later use by another part of your code, and the object has not only distinct data but distinct behavior, then polymorphism is likely to help.
One of the most useful features of polymorphism lies in how it supports the addition of new types to existing code, types not foreseen at the time the original code was designed. Thus, polymorphism may often be more a matter of maintenence practice than of logical conception.
In many substantial programs, polymorphism is not needed at all, and in programs of less than 2000 lines it seldom is needed or helpful. Polymorphism addresses a practical design problem that tends to arise in programs 10,000 lines or longer -- which is precisely why the examples typically given of polymorphism tend to be so uselessly contrived. It really is hard to see the need for polymorphism until one has written a program big enough for it.

No comments:

Post a Comment