Dynamic Polymorphism Example in C#
Polymorphism are of two types
1. Static or Compile Time polymorphism
2. Dynamic or Run Time polymorphism
Run time Polymorphism also known as method overriding. In this type the call to a function is decided at the runtime.
Lets have an example of Dynamic Polymorphism in C#.
In the First Example we will see that what happens when we dont use the concept of Dynamic Polymorphism.using System;
namespace Normal
{
public class BaseClass
{
public void Func1()
{
System.Console.WriteLine("Func1 of base class");
}
public void Func2()
{
System.Console.WriteLine("Func2 of base class");
}
}
class ChildClass : BaseClass
{
public new void Func1() // new defined
{
System.Console.WriteLine("Func1 of child class newly defined");
}
public void Func3()
{
System.Console.WriteLine("Func3 of child class");
}
public static void Main(string[] args)
{
BaseClass b1 = new BaseClass();
ChildClass c1 = new ChildClass();
b1.Func1();
// Invoke base class function because b1 is a Base clas Object
c1.Func1();
// Invoke Child class function because c1 is a Child clas Object
b1 = new Child();
b1.Func1();
// Invoke base class function even b1 pointing child class object
//because child class do not override it.
// So always base class function preferred through base class
// reference variable even child class redefined it
System.Console.Read();
}
}
}
Now if base class give permission to child class to override and child class overrided it then dynamically child class function will be associated with the base class refernce variable when referencing child class object. (Dynamic Polymorphism)
Now if we will use the Dynamic Polymorphism then...using System;
namespace DynamicPolymorphism
{
public class BaseClass
{
public virtual void Func1() // Give Permission to the child class to
//override it.
{
System.Console.WriteLine("Func1 of base class");
}
public void Func2()
{
System.Console.WriteLine("Func2 of base class");
}
}
class ChildClass : BaseClass
{
public override void Func1() // Overrided
{
System.Console.WriteLine("Func1 of child class newly defined");
}
public void Func3()
{
System.Console.WriteLine("Func3 of child class");
}
public static void Main(string[] args)
{
BaseClass b1 = new BaseClass();
ChildClass c1 = new ChildClass();
b1.Func1();
c1.Func1();
b1 = new Child();
b1.Func1(); // Call Child Class Func1()
System.Console.Read();
}
}
}
Test the output to understand the differences.
Polymorphism
Same function perform different operations i.e Function in Base class is same as in the derived class, but function in each derived classes perform different operations
Method Overloading
Method with same name but differ only with arguments in a
class is called method overloading.
Their is No base class or Derived class in overloading.
only at overriding base and derived classes are found
Method Overriding
Method overriding occurs when derived class or child class declares a method that has the same type arguments as a method declared by one of its base class or super class
Compile Time Polymorphism
when you have several methods with same name and different parameters compiler has to decide how to select which method has to run based on the arguments
Generally compile time is a process of compiling source code, and Run time is actually executing your program.
Overloading
Same function name different argument or parameters
Overriding
same function name & same argument as like in base class
***** Examples *****
Polymorphism (Method overloading) simple
// Methods receive arguments depend upon the parameter
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication34
{
class Print
{
public void display (string name) // method overloading
{
Console.WriteLine("Your name is : " + name);
}
public void display(int age, float marks)//methodoverloading
{
Console.WriteLine("Your age is : " + age);
Console.WriteLine("Your marks are :" + marks);
}
static void Main(string[] args)
{
Print obj = new Print();
obj.display("George");
obj.display(34, 76.50f);
Console.ReadLine();
}
}}
/* why we using the line means if we debug program it will execute then suddenly closed , if we use console.ReadLine() it execute and wait until user enter any key */
***** Example 2 *****
Polymorphism method overriding
Method overriding is possible only in derived classes, but
not within the same class
When derived class needs a method with same signature as in
base class, but wants to execute different code than
methodoverriding will be used
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication36
{
public class Drawing
{
public virtual void Draw()
{
Console.WriteLine("I'm just a generic drawing object.");
}
}
/* Overriding methods must have the same method name and parameters,as the virtual base class method it is overriding */
public class Line : Drawing
{
public override void Draw()
{
Console.WriteLine("I'm a Line.");
}
}
public class Circle : Drawing
{
public override void Draw()
{
Console.WriteLine("I'm a Circle.");
}
}
public static void Main()
{
Drawing obj = new Drawing();
obj.Draw();
//Obj created only for base class
//in method overriding
}
}
// please give ratings and points for my Hard work
// please give ratings and points for my Hard work