2. Polymorphism: Literal meaning
• polymorphism in Latin word which made up of
'poly' means many and 'morphs' means forms.
• From the Greek:
– Polus + Morphe = Polumorphos
(many ) (shape/form)
• This is something similar to a word having several
different meanings depending on the context
3. A simple word ‘Cut’ can have different meaning
depending where it is used
4. Polymorphism, In Context Of OOP
• Polymorphism is about an objects ability to
provide context when methods or operators
are called on the object.
• In OOP, Polymorphism is the characteristic of
being able to assign a different meaning to a
particular symbol or "operator" in different
contexts - specifically, to allow an entity such
as a variable, a function, or an object to have
more than one form.
5. Polymorphism
• When a program invokes a method through a
superclass variable,
– the correct subclass version of the method is
called,
– based on the type of the reference stored in the
superclass variable
• The same method name and signature can
cause different actions to occur,
– depending on the type of object on which the
method is invoked
5
6. Polymorphism
• Polymorphism enables programmers to deal
in generalities and
– let the execution-time environment handle the
specifics.
• Programmers can command objects to behave
in manners appropriate to those objects,
– without knowing the types of the objects
– (as long as the objects belong to the same
inheritance hierarchy).
6
7. The Meaning of the word.
• Example: The operator + has a different
meaning in the expression 2 + 3 (add two
integers) than in 1.7 + 3.3 (add two floating
point numbers)
7
8. Types of polymorphism
• 1. compile time polymorphism
( function and operator overloading)
• 2.run time polymorphism
(virtual functions)
9. Method & Function Overloading
• Overloading a function simply means, that a
function is not only defined its name but by its
name and parameter types.
These three
• The following functions are different in C++: are
methods
– Int area(int i, int k); different.
– void area( float i, float k);
– Float area();
9
10. Method Overloading
using System;
class maindemo
using System.Collections.Generic; {
using System.Linq; static void Main(string[] args)
using System.Text; {
Area a1= new Area();
a1.length=10;
a1.breadth=20;
class Area{ int area1 =a1.calc(10,20);
public double length; double area2= a1.calc(10.5, 20.2);
public double breadth; a1.calc();
Console.WriteLine();
Console.Write("Area =" + area1);
public int calc(int l, int b)
Console.WriteLine();
{ return (l * b); } Console.Write("Area =" + area2);
public double calc(double l,double b) Console.WriteLine();
{ return (l * b); } }
public void calc() }
{ Console.Write("Area =" + 40 ); }
}
12. Method overriding
class car:vechile
using System;
{
Using System.Collections.Generic; public override void display()
using System.Linq; {
using System.Text; Console.WriteLine(" It is from
child Car");
}
}
class vechile class maindemo
{ {
public virtual void display() static void Main(string[] args)
{ {
vechile v1= new car();
Console.WriteLine(“It is from
v1.display();
parent Vechile"); }
} }
}
14. Run-time polymorphism
• Run-time polymorphism, also called dynamic
binding, or late binding is often considered as
the object oriented feature of C#.
• Dynamic means objects are created at run
time
• Dynamic binding offers greater flexibility and a
higher level of abstraction than static binding
because it is done "on the fly" when a
program executes
15. Static typing & Dynamic binding
• Static typing means that the • Dynamic binding means that
legality of a member the address of the code in a
function invocation is member function invocation
checked at the earliest is determined at the last
possible moment: by the possible moment: based on
the dynamic type of the
compiler at compile time.
object at run time. It is called
The compiler uses the static "dynamic binding" because
type of the pointer to the binding to the code that
determine whether the actually gets called is
member function accomplished dynamically
invocation is legal. (at run time).
15
16. Static typing & Dynamic binding
• With static binding, you • Dynamic binding offers
get better run time greater flexibility and a
efficiency because the higher level of
compiler can actually abstraction than static
optimize the code binding because it is
before running it. done "on the fly" when
a program executes.
• The CLR knows how • The CLR doesn't know
much memory to take how much memory to
up for the static method take up for the dynamic
object method object