2. 4. POLYMORPHISM
means many and means form.
Polymorphism is the ability of an entity (e.g. variable, class, method,
object, code, parameter, etc.) to take on different meanings in different
contexts.
3. POLYMORPHISM CATEGORIES
: a piece of code works are finite and all
those types must be known when the code is written
Overloading Polymorphism
Coercion polymorphism
: a piece of code is written in such a way
that it works for infinite number of types
Inclusion polymorphism
Parametric polymorphism
4. OVERLOADING POLYMORPHISM
When a or an has at least two definitions that work
on different type
/*********************************Method Overloading**************************/
public class MathUtil {
public static int max(int n1, int n2) {
/* Code to determine the maximum of two integers goes here */
}
public static double max(double n1, double n2) {
/* Code to determine the maximum of two floating-point numbers goes here */
}
public static int max(int[] num) {
/* Code to determine the maximum of an array of int goes here */
}
}
/*************************************************client code*************************/
int max1 = MathUtil.max(10, 23); // Uses max(int, int)
double max2 = MathUtil.max(10.34, 2.89); // Uses max(double, double)
int max3 = MathUtil.max(new int[]{1, 89, 8, 3}); // Uses max(int[])
/*************Operator Overloading********************/
int n1 = 10 + 20; // Adds two integers
double n2 = 10.20 + 2.18; // Adds two floating-point numbers
String str = "Hi " + "there"; // Concatenates two string
5. COERCION POLYMORPHISM
When a type is implicitly converted (coerced) to another type
automatically even if it was not intended explicitly
int num = 707;
double d1 = (double)num; // Explicit conversion of int to
double double d2 = num; // Implicit conversion of int to double (coercion)
6. INCLUSION POLYMORPHISM
When a piece of code that is written using a type works for all its subtypes
void processDetails(Person p) {
/* Write code using the formal parameter p, which is of type Person.
The same code will work if an object of any of the subclass of Person is passed to this method. */
}
/************************************client code*************************************************/
Person p1 = new Person();
Employee e1 = new ;
Customer c1 = create a Customer object;
processDetails(p1); // Use Person type
processDetails(e1); // Use Employee type, which is a subclass of Person
processDetails(c1); // Use Customer type, which is a subclass of Person
7. NOTE: METHOD HIDING
Method hiding means subclass has defined a class method with the
same signature as a class method in the superclass. In that case
the method of superclass is hidden by the subclass.
8. PARAMETRIC POLYMORPHISM
Parametric polymorphism is achieved by using a type variable when
writing the code, rather than using any specific type
It is also called “true” polymorphism because it lets you write true
generic code that works for any types (related or unrelated)
9. PARAMETRIC POLYMORPHISM
TYPES
Means that you can use only the type originally specified;
so an invariant generic type parameter is neither covariant nor
contravariant.
Enables you to use a more derived type than originally
specified.
Enables you to use a more generic (less derived)
type than originally specified.
IEnumerable<Derived> d = new List<Derived>(); //IEnumerable<out T>
IEnumerable<Base> b = d;
Action<Base> b = (target) => { Console.WriteLine(target.GetType().Name); }; //Action<in T>
Action<Derived> d = b;
d(new Derived());
10. DISPATCH
In simple words, it is a way how the programming
language calls a method or a function.
•Static Dispatch
•Dynamic Dispatch
11. STATIC DISPATCH
Every method is known at the compile time
public interface IBar {}
public class Bar : IBar {}
public sealed class FooBar : Bar {}
public static class ConsolePrinter
{
public static void Print(IBar item){Console.WriteLine("IBar"); }
public static void Print(Bar item){ Console.WriteLine("Bar"); }
public static void Print(FooBar item){
Console.WriteLine("FooBar"); }
}
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
ConsolePrinter.Print(bar);
ConsolePrinter.Print(foo);
ConsolePrinter.Print(ibar);
// prints Bar
// prints FooBar
// prints IBar, doesn’t
work
13. SINGLE DYNAMIC DISPATCH
Overriding: Single ones use just one parameter to select a method
public class SurveyBase{
public virtual void DoSurvey(){
Console.WriteLine("Base Class");
}
}
public class Survey : SurveyBase{
public override void DoSurvey() {
Console.WriteLine("Derived Class");
}
}
/*****************************Client
Code**************************************/
SurveyBase base = new Survey();
base.DoSurvey(); // Prints "Derived Class"
14. MULTIPLE DYNAMIC DISPATCH
The multiple ones can take advantage of as many parameters they want
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
IBar[] items = { bar, foo, ibar };
foreach (var item in items)
{
ConsolePrinter.Print(item);
}
// prints IBar
// prints IBar
// prints IBar
var bar = new Bar();
var foo = new FooBar();
IBar ibar = new FooBar();
IBar[] items = { bar, foo, ibar };
foreach ( item in items)
{
ConsolePrinter.Print(item);
}
// prints Bar
// prints FooBar
// prints FooBar
Editor's Notes
When one task is performed by different ways i.e. known as polymorphism. For example: to convenes the customer differently, to draw something e.g. shape or rectangle etc.