We will learn about polymorphism in c# with an example and Type of polymorphism in c# in this article. That will help you understand the concepts of polymorphism in c# with real time example for fresher as well as experienced people.
What is polymorphism?
It is used to one object behaving as multiple forms. Or polymorphism means one name many forms.
Real Time Example:
1. A person behaves as an employee in the office, that the same person behaves as a father in the house, that the same person behaves as a customer in the shopping malls.
Description: a person is one object. It behaves as an employee in office as well as the father in the house. So the concept is here one name many forms.
Type of polymorphism
Type of polymorphism
- Compile time polymorphism / Static polymorphism
- Run time polymorphism / Dynamic polymorphism
- Static or Compile time polymorphism:
In the compile time polymorphism compiler know which method is going to call at compile time called compile-time or Static polymorphism.
The static or compile time polymorphism can be achieved by using “function overloading”.Where the compiler know which overloaded method is going to call at compile time.
function overloading means the same method name but the type of parameter should be different.
Example 1: This is the basic example to understand the concept of method overloading.
using System;
namespace MethodOverloading
{
class Program
{
// Method Overloading
public int Add(int x, int y)
{
return x + y;
}
public int Add(int x, int y, int z)
{
return x + y + z;
}
static void Main(string[] args)
{
Program prog = new Program();
Console.WriteLine("Addition of Number is {0}", prog.Add(10, 20));
Console.WriteLine("Addition of Number is {0}", prog.Add(10, 20, 30));
Console.ReadLine();
}
}
}
Real time example of Static or Compile time polymorphism
Example 2: This is the Real time example to understand the concept of method overloading.
using System;
namespace MethodOverloading
{
public class User
{
public void UserLogin(string uname, string password)
{
if (uname == "user" && password == "user@123")
{
// Get data from datbase.
Console.WriteLine("{0} is valid and Loged sucessfully.........", uname);
}
else
{
Console.WriteLine("Invalid User Name or Password!");
}
}
public void UserLogin(string uname, string password, string role)
{
if (role == "Admin" && (uname == "admin" && password == "admin@123"))
{
// Get data from datbase.
Console.WriteLine("{0} is valid and Loged sucessfully.........", role);
}
else
{
Console.WriteLine("Invalid User Name or Password!");
}
}
}
class Program
{
static void Main(string[] args)
{
User user = new User();
user.UserLogin("user", "user@123");
user.UserLogin("admin", "admin@123", "Admin");
Console.ReadLine();
}
}
}
Output:
Note: In the compile time polymorphism the compiler known at compile time which method gets called.
- Dynamic or Run time polymorphism:
1. Whenever the object behaves as multiple forms at run time then it is said to be a run time polymorphism.
2. Runtime polymorphism can be achieved by using the method overriding. The method overriding can be done in parent and child class.
3. The Method overriding means method name and list of the parameter should be the same in parent and child class.
4. It is also called late binding.
Example 2:
More Information Please watch my video as follow:
using System;
namespace MethodOverriding
{
public class Employee
{
public virtual void PrintName(string fname, string lname)
{
Console.WriteLine("Full Name {0}", fname + " " + lname);
}
}
public class Child : Employee
{
public override void PrintName(string fname, string lname)
{
Console.WriteLine("Employee Full Name {0}", fname + " " + lname);
}
}
public class MethodOverridingDemo
{
static void Main(string[] args)
{
Employee emp;
emp = new Child();
emp.PrintName("Shri", "VT");
emp = new Employee();
emp.PrintName("Shrimant", "VT");
Console.ReadLine();
}
}
}
Real time example of Dynamic or Run time polymorphism
Example 3:
using System;
namespace RealTimeMethodOverriding
{
public class User
{
public virtual void UserLogin(string uname, string password, string role)
{
if (role == "user" && (uname == "user" && password == "user@123"))
{
Console.WriteLine("{0} is valid and Loged sucessfully.........", role);
}
else
{
Console.WriteLine("Invalid User Name or Password!");
}
}
}
public class Admin : User
{
public override void UserLogin(string uname, string password, string role)
{
if (role == "Admin" && (uname == "Admin" && password == "admin@123"))
{
Console.WriteLine("{0} is valid and Loged sucessfully.........", role);
}
else
{
Console.WriteLine("Invalid User Name or Password!");
}
}
}
public class SuperAdmin : User
{
public override void UserLogin(string uname, string password, string role)
{
if (role == "SuperAdmin" && (uname == "SuperAdmin" && password == "admin@123"))
{
Console.WriteLine("{0} is valid and Loged sucessfully.........", role);
}
else
{
Console.WriteLine("Invalid User Name or Password!");
}
}
}
class Program
{
static void Main(string[] args)
{
User user;
user = new Admin();
user.UserLogin("Admin", "admin@123", "Admin");
user = new SuperAdmin();
user.UserLogin("SuperAdmin", "admin@123", "SuperAdmin");
Console.ReadLine();
}
}
}
Output:
As per the above example, you understand that user is one object it behaves as admin and super admin at run time. It is said to one object behave as multiple forms as run time called run time polymorphism.
I hope you understood the basic concept of polymorphism. Please add your valuable comment and share this article other people can help us to understand the concept of polymorphism with real-time use.
Following are the previews article on OOPs
step by step explanation good article.
ReplyDeleteThank you Sandeep.
DeleteGreat explanation step by step...Thanks for sharing..
ReplyDeleteThank you so much Narayan
DeleteNice explain
ReplyDeleteThank you so much...
DeleteNice article..
ReplyDeleteThank for reading...
DeleteThank you so much...
ReplyDeletethank you its so helpful.
ReplyDeleteGood explanation
ReplyDeleteThank you so much!
DeleteGood Explanation
ReplyDeleteThank you
Delete