All IT Courses 50% Off
JAVA Tutorials

Polymorphism in Object-Oriented Programming

Polymorphism is another important feature of OOP. The word polymorphism comes from a Greek which means having many shapes or many forms yet a single name. Polymorphism can be achieved with the Overloading of Functions and overloading of the operators. This lets you create more than one form definition for the given single function name or the operator.

Function Overloading: It means that you can have several functions that have the same function name but are different in signatures.

For  instance, you can declare in C++

int small (int a, int b);

Float small (float x, float y, float z) : /to return Smallest of 3 float

void smal1l (double, double);

Here the function small ( ) is said to be overloaded !!!

Dynamic Binding:

Selection as to which of the overloaded member functions must be invoked is solely determined by the compiler by matching the type of and the number of arguments. Thus the compiler is able to select and like the appropriate overloaded function for the particular function call at the compile time itself. This kind of linking mechanism is known as early binding or static binding. Thus an object is bound to its function at the compile time itself.

C + + also allows late binding or dynamic binding at the run time of a program by means of special member functions called virtual functions. The key concept is that virtual function calls are attended at runtime hence the name late binding. 

Polymorphism in C++ is accomplished with virtual functions. It enables the objects of various class types to behave differently to the same function or the name calling. In other words, virtual functions permit us to define many versions of the same function in a number of derived classes (from a base class) called class hierarchy.  Thereby which version of the function is to be executed with a particular object is being determined at run-time. This kind of Polymorphism is termed Run-time polymorphism and is used extensively in implementing Class Inheritance. Whereas polymorphism offered with Function Overloading and Operator overloading is termed Compile-time polymorphism.

Let us consolidate this discussion into a formal definition of Polymorphism:

The term polymorphism in C++ refers to the ability of functions in a class hierarchy to exhibit different behavior for the same function (name) call depending on the type of the object for which that function is called and without regard to the class type of the object.

Inheritance:

We are all familiar with the terms heredity and hereditary inheritance. For instance, if a son or daughter possesses exclusive characteristics of his/her parents’ father and mother then we simply utter the words “it’s hereditary” i.e., those specific characteristics are inherited from their parents.

Sometimes if the grandfather observes that his grandson behaves exactly like him but has acquired similar but slightly different characteristics, then also grandfather quite happily expresses himself that his grandson has acquired certain behaviors and characteristics of his own family members and which is purely heredity.

In a similar way, the C++ class can inherit the characteristics of another class. An important objective of OOP i.e., providing reusable program code is achieved via Class Inheritance. Class inheritance is the technique that is used to build new, derived classes that inherit the data members’ data representation and functions or the behavior from one or more previously-defined base classes. At a similar time, the class inheritance also provides for possibly redefining and/or adding new data members and functions to the new derived class. Thus deriving a series of subclasses derived from one source superclass (base class) creates a hierarchy of classes or simply class hierarchy also known as hierarchical inheritance.

Questions:

1. Explain Polymorphism.

Facebook Comments

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button