Object Oriented Programming: Polymorphism and Abstract Classes
Object Oriented Programming: Polymorphism and Abstract Classes
Polymorphism and
Abstract Classes
Introduction
• The word polymorphism means having many
forms. In simple words, we can define
polymorphism as the ability of a message to be
displayed in more than one form.
• Real life example of polymorphism, a person at a
same time can have different characteristic. Like a
man at a same time is a father, a husband, a
employee. So a same person posses different
behavior in different situations. This is called
polymorphism.
Types of Polymorphism
• Compile time Polymorphism
– This type of polymorphism is achieved by function
overloading or operator overloading.
• Runtime Polymorphism
– This type of polymorphism is achieved by Function
Overriding.
Run Time Polymorphism
• Function overriding on the other hand occurs
when a derived class has a definition for one
of the member functions of the base class.
That base function is said to be overridden.
Early Binding
Early Binding Cond..
• The reason for the incorrect output in both
previous programs is that the call of the function
getName() and speak() is being set once by the
compiler as the version defined in the base class.
This is called static resolution of the function call,
or static linkage - the function call is fixed before
the program is executed. This is also sometimes
called early binding because the function
resolution is done during the compilation of the
program.
Late Binding
• But now, let's make a slight modification in
our program
• keyword virtual so that it looks like this −
Virtual Functions and Polymorphism