Polymorphism
- To Invoke Method Of Derived Class through Base Class during Runtime.
- To Provide Different implementations of methods that are called through the same name.
- There are Two Types Of Polymorphism
1)Compile Time Polymorphism
2)Run Time Polymorphism
Compile Time Polymorphism
- It can be called as Method Overloading.
- This Method has Same Name with different Arguments.
- Use of Method Overloading
1)To eliminate complexity
2)To Increase performance
Example
class Program
{
static void Main(string[] args)
{
Overloaded("Sabari");
Overloaded("Sabari","Mahesh");
Overloaded("Sabari","Mahesh ", "P M");
Console.ReadLine();
}
static void Overloaded(string value)
{
Console.WriteLine(value);
}
static void Overloaded(string value, string value1)
{
Console.WriteLine(value+value1);
}
static void Overloaded(string value, string value1, string value2)
{
Console.WriteLine(value+ value1 + value2)
}
}
Run Time Polymorphism
Example 1:
Class BaseClass consists of function Show().
Class DerivedClass hides the function Show()
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}
class DerivedClass : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
Output
Baseclass::Show
Show() of class BaseClass is executed because the reference variable b refers to the object of class BaseClass.
Example 2
Creating an object of Derived class DerivedClass and storing its reference in the reference variable bc of typeBaseClass. This is valid in C#.
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}
class DerivedClass : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
Output
Baseclass::Show
Baseclass::Show
bc is a reference of type Baseclass, the function Show() of class Baseclass will be invoked, no matter whom bc refers to.
Example 3
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
bc.Show();
bc = new DerivedClass2();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}
class DerivedClass : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
class DerivedClass2 : Baseclass
{
new public void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}
Output
Baseclass::Show
Baseclass::Show
Baseclass::Show
it invokes the functions of the class that matches its Reference bc,
bc contains the reference to a particular derived class object, then its supposed to invoke the function of that class
Example 4
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new Baseclass();
bc.Show();
bc = new DerivedClass();
bc.Show();
bc = new DerivedClass2();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public virtual void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}
class DerivedClass : Baseclass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
class DerivedClass2 : DerivedClass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}
Output
Baseclass::Show
DerivedClass::Show
DerivedClass2::Show
The function Show() of Base class Baseclass is declared as virtual, while the implementation of Show() in successive Derived classes is decorated with the modifier override. Next, we succesively create objects of each class and store their reference in base class reference variable Baseclass and invoke Show(). The rite versions of Show get invoked based on the object the reference variable refers to.
Example 5
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new DerivedClass2();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public virtual void Show()
{
System.Console.WriteLine("Baseclass::Show");
}
}
class DerivedClass : Baseclass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
class DerivedClass2 : DerivedClass
{
public new void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}
Output
DerivedClass::Show
We Know that DerivedClass2 was called.
But, The Method inside the Class (DerivedClass2) will not be displayed; because it is hidden. Also, we didn't override the method
Example 6
We Have 5 Classes: One Baseclass and Four DerivedClasses
The BaseClass have a Virtual method
The First Derived class Have a method with "new" Key word ,
i.e;Data of this class will be hidden.
This is the Major purpose Of Using New Keyword.
We Override All The other Derived classes except the First Derived class.
Note:
You Can't Use This:
public new void Show()
DerivedClass2.Show() cannot override inherited member DerivedClass.Show() because it is not marked virtual, abstract, or override
You Can Use this
public virtual new void Show()
Because this is also a Virtual Method
class Program
{
static void Main(string[] args)
{
Baseclass bc;
bc = new DerivedClass4();
bc.Show();
Console.ReadLine();
}
}
class Baseclass
{
public virtual void Show()
{
System.Console.WriteLine("");
}
}
class DerivedClass : Baseclass
{
public virtual new void Show()
{
System.Console.WriteLine("DerivedClass::Show");
}
}
class DerivedClass2 : DerivedClass
{
public override void Show()
{
System.Console.WriteLine("DerivedClass2::Show");
}
}
class DerivedClass3 : DerivedClass2
{
public override void Show()
{
System.Console.WriteLine("DerivedClass3::Show");
}
}
class DerivedClass4 : DerivedClass3
{
public override void Show()
{
System.Console.WriteLine("DerivedClass4::Show");
}
}
Output
Baseclass::Show
Baseclass::Show
Conclusion
We have learned about polymorphism. I hope that my article was helpful.