Skip to main content

Polymorphism

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


Comments

Popular posts from this blog

Media Player in C# Visual Studio By Rohit Programming Zone

Media Player  in C# Visual Studio   Source Code ----------------------------------------------------------------------------------------------------------------------------- using System; using System.Windows.Forms;   namespace Media_Player_in_C_Sharp_By_Rohit_Programming_Zone {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         string [] paths, files;           private void btn_open_Click( object sender, EventArgs e)         {             OpenFileDialog ofd = new OpenFileDialog ();   ...

ImageEditor in C# Visual Studio 2017

ImageEditor in C # Start a new project in Visual Studio > choose Visual C# then click on Windows Desktop then select WindowsForm App(.Net Framework)  and name it as ImageEditor then press ok. First, go to the Form1 properties  S ection and set the Size as following. Form1-size=952, 607 Now Add 3 panels from the toolbox and set the properties as follows. After setting the properties it will look as follows. Now Add 2-picture box from the toolbox and go to the properties and set the properties as follows. Now add 15-Button from the toolbox. Now add 3-textbox from the toolbox. Now add 3-trackbar and 5-label from the toolbox. Now go to the properties section and check all the Components are named correctly or not. Once  all the Components are named correctly then  move to the coding part. (Now the Coding Part) Now go to the Form1 code view and  ...

Microsoft tools Download

1-Visual Studio Code Visual Studio Code is a  fast and free  source code lite editor that can be used with the various programming languages. Download 2-Visual Studio 2019 Community Download 3-Microsoft Visual Studio 2010 Microsoft Visual Studio 2010 Development environment specialized in Windows systems. Download 4-VISUAL STUDIO 2013 EXPRESS FOR WEB Visual Studio Express 2013 for Web provides the core tools for creating compelling, innovative web applications and services. This includes the ASP.NET editors for HTML5, CSS3, JavaScript, etc. Download 5-Piskel Download 6-Pichon Download 7-Microsoft XNA Framework Redistributable 4.0 Description The XNA Framework has all the necessary runtime components to execute a game, which was developed using Microsoft XNA Game Studio 4.0. Version 4.0 of the framework contains improved functionality and new features.