Polymorphism in C#
Polymorphism is a concept where one name can have numerous structures. In other words, one object has many features or has one name with multiple functionalities. Polymorphism gives the ability to a class to have various users with a similar name. In C# Programming there are two sorts of polymorphism.
1-Static polymorphism
2-Dynamic polymorphism
1-Static polymorphism
In Static polymorphism, the reaction to a capacity is resolved at compile time that why It's known as Compile Time Polymorphism. The mechanism of connecting a function with an item during compile time is called early binding. Method overloading is a concept where we utilize a similar strategy name multiple times in a similar class, yet various parameters. its depends upon the parameters we pass, it is chosen at compile time only. A similar strategy name with similar parameters is an error and it is an instance of duplication of functions which C# does not allows. Overloading, the function has an equivalent name yet various marks.so let's clear out through the program as follows.
Program
using System;
namespace Static_Polymorphism
{
class Print
{
/* Utilizing similar class name multiple times with different strategy */
public void Display( string s )
{
Console.WriteLine("My Name is:" + s);
}
public void Display(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("Sum of two number:" + c);
}
public void Display(double f)
{
Console.WriteLine("float number :" + f);
}
static void Main(string[] args)
{
Print p = new Print();
p.Display("Rohit"); /* Chosing at compile time */
p.Display(50, 9);
p.Display(84.004);
Console.ReadKey();
}
}
}
Output
2-Dynamic polymorphism
In Dynamic polymorphism, the reaction to a capacity picked at run-time.C# enables you to make unique classes that are utilized to give partial class execution of an interface. Usage is finished when a derived class acquires from it. Abstract classes contain dynamic techniques, which are executed by the derived class and Dynamic polymorphism is executed by abstract classes and virtual functions.
So let's see the program below which demonstrate Dynamic polymorphism.
Program
using System;
namespace Dynamic_Polymorphism
{
public class Light_Bulb /* Base class*/
{
public virtual void Switch()
{
Console.WriteLine("Switch_OF");
}
}
public class Connection : Light_Bulb /* Derived class*/
{
public override void Switch()
{
Console.WriteLine("Switch_ON");
}
}
class Program
{
static void Main(string[] args)
{
Light_Bulb open = new Connection(); /* Utilizing functions from Derived class*/
open.Switch();
Light_Bulb close = new Light_Bulb(); /* Utilizing functions from Base class*/
close.Switch();
Console.ReadKey();
}
}
}
Output
Polymorphism is a concept where one name can have numerous structures. In other words, one object has many features or has one name with multiple functionalities. Polymorphism gives the ability to a class to have various users with a similar name. In C# Programming there are two sorts of polymorphism.
1-Static polymorphism
2-Dynamic polymorphism
1-Static polymorphism
In Static polymorphism, the reaction to a capacity is resolved at compile time that why It's known as Compile Time Polymorphism. The mechanism of connecting a function with an item during compile time is called early binding. Method overloading is a concept where we utilize a similar strategy name multiple times in a similar class, yet various parameters. its depends upon the parameters we pass, it is chosen at compile time only. A similar strategy name with similar parameters is an error and it is an instance of duplication of functions which C# does not allows. Overloading, the function has an equivalent name yet various marks.so let's clear out through the program as follows.
Program
using System;
namespace Static_Polymorphism
{
class Print
{
/* Utilizing similar class name multiple times with different strategy */
public void Display( string s )
{
Console.WriteLine("My Name is:" + s);
}
public void Display(int a, int b)
{
int c;
c = a + b;
Console.WriteLine("Sum of two number:" + c);
}
public void Display(double f)
{
Console.WriteLine("float number :" + f);
}
static void Main(string[] args)
{
Print p = new Print();
p.Display("Rohit"); /* Chosing at compile time */
p.Display(50, 9);
p.Display(84.004);
Console.ReadKey();
}
}
}
Output
2-Dynamic polymorphism
In Dynamic polymorphism, the reaction to a capacity picked at run-time.C# enables you to make unique classes that are utilized to give partial class execution of an interface. Usage is finished when a derived class acquires from it. Abstract classes contain dynamic techniques, which are executed by the derived class and Dynamic polymorphism is executed by abstract classes and virtual functions.
So let's see the program below which demonstrate Dynamic polymorphism.
Program
using System;
namespace Dynamic_Polymorphism
{
public class Light_Bulb /* Base class*/
{
public virtual void Switch()
{
Console.WriteLine("Switch_OF");
}
}
public class Connection : Light_Bulb /* Derived class*/
{
public override void Switch()
{
Console.WriteLine("Switch_ON");
}
}
class Program
{
static void Main(string[] args)
{
Light_Bulb open = new Connection(); /* Utilizing functions from Derived class*/
open.Switch();
Light_Bulb close = new Light_Bulb(); /* Utilizing functions from Base class*/
close.Switch();
Console.ReadKey();
}
}
}
Output
No comments:
Post a Comment