The knowledge sharing zone - Guruzon.com

What is Polymorphism

This page contains the detailed explanation about What is Polymorphism, What is use of it and How its related to Inheritance. It also describes various polymorphic object or operation in details with example.
  • Related Topics:
reviews

Definition of Polymorphism

  • Polymorphism means the ability to take on many forms, The term is applied both to objects and to operations.
  • Polymorphism is tightly coupled to inheritance and is one of the most powerful advantages to object-oriented technologies.
  • Polymorphism is a Greek word that means Many Shapes.

Polymorphism : Polymorphic object details with example

  • Suppose a financial company handles different subtypes of Funds, such as an Asia Fund, Domestic Fund, and so on.
  • Each with its own idiosyncrasies. 
  • We model this situation using a generalized class(Fund) and a specialized class for each subtype of Fund with difference operations and states.
  • When the operation is actually executed, though the Fund object will take on one of many forms for example, an Asia Fund or a Domestic Fund.
  • In other words, the Fund object is polymorphic.

Polymorphism : Polymorphic operation details with example

  • Polymorphic operation - "One Operation, Many Methods".
  • A polymorphic operation is one whose method may take on many forms based on the class of the object carrying it out.
  • A polymorphic operation is one whose method may take on many forms based on the class of the object carrying it out.
  • Continuing with the same example, since all the Fund subtypes have to be able to accept deposits, we can define a Fund operation called accept deposit. 
  • This operation is inherited by all the specializations. 
  • We can also specify a method for this Fund operation that will be inherited by the specializations. 
  • But what if one or more of the specializations for example,the Asia Fund—uses a different procedure for accepting deposits? 
  • In this case, we can add documentation to the Asia Fund class that describes a method that overrides the one inherited from the generalized class.

For example,

  • The method described might involve supplementary charges. In practice, when capital investment causes a Fund to perform an accept deposit operation, the method that is used to carry out the operation will take on one of many forms. 
  • This is what is meant by a polymorphic operation.
  • With polymorphic operations, the selection of the method depends on which particular class (Asia Fund, Domestic Fund, and so on) is carrying it out.
  • Now let's look at an Object Oriented Programming example. 
  • By using polymorphism and grouping the Circle into a Shape category, Shape figures out that it is a Circle and knows how to print itself (see below figure):


Shape.print();  // Shape is actually a Circle 
Shape.print();  // Shape is actually a Square

The important thing to understand here is that the call is identical;the context of the shape dictates how the system reacts.

Polymorphism - One Interface, Many Implementations

  • Polymorphism means "one interface, many possible implementations." 

For example,

  • Cars are designed with polymorphism in mind.
  • They all use the same interface-an accelerator pedal-to change speed, even though the internal method may differ from model to model.
  • The auto industry designs cars this way so that the drivers do not have to learn a new interface for each new model of car.

Two types of polymorphism

  1. Static (compile time) polymorphism
  2. Dynamic (run time) polymorphism

Static polymorphism  means that the information required to call a function is available at compile time itself.

  • Hence function calls can be resolved at compile time. The exact function to call is determined by the difference in either the number or type of functions parameters.
  • Static polymorphism is achieved through function overloading, operator overloading and even function templates (in Java/C++).
  • It is always faster and efficient than dynamic polymorphism because the run time cost of function call resolution is avoided.

For example,

  • An employee object may have two print() methods one taking no arguments and one taking a prefix string to be displayed along with the employee data.
  • Given these interfaces, when the print() method is called without any arguments, the compiler, looking at the function arguments knows which function is meant to be called and it generates the object code accordingly.

Dynamic polymorphism  means that the information required to call a function is not known until run time.

  • Hence function call resolution takes place at run time. This mechanism is achieved through inheritance and virtual functions.  
  • If a derived class needs to redefine a particular method defined in the base class, the method is declared as virtual in the base class and redefined, to fit its own needs, in the derived class.
  • The virtual function in the base class basically defines the interface of that function. Each derived class of the base class with virtual methods can redefine the methods with its own implementation.
  • A base-class reference (and pointer in case of C++) can be used to point to an object of any class derived from that base.
  • When a base reference or pointer points to a derived object that contains a virtual function, the exact function to call is determined based upon the type of object calling the method at runtime.
  • Thus, when different derived objects are pointed to by the base class reference or pointer, appropriate versions of the virtual function are executed. 
  • Since the call is resolved at runtime, dynamic polymorphism results in slightly slower execution of the program.

For example,

  • Consider an application that serializes and de-serializes different types of documents.
  • We can have ‘Document’ as the base class and different document type classes deriving from it. E.g. XMLDocument , WordDocument , etc.
  • Document class will define ‘ Serialize() ’ and ‘ De-serialize() ’ methods as virtual and each derived class will implement these methods in its own way based on the actual contents of the documents.
  • When different types of documents need to be serialized/de-serialized, the document objects will be referred by the ‘ Document’  class reference (or pointer) and when the ‘ Serialize() ’ or ‘ De-serialize() ’ method are called on it, appropriate versions of the virtual methods are called.

You would also like to read:

Comments :


thanks for this information...i want this info with example pgms in java and explanation of pgms also

up vote icon down vote icon

awesome............

up vote icon down vote icon

I need some example programs, with this concepts.......... M

up vote icon down vote icon
Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow