Home > DeveloperSection > Articles > Polymorphism in C#.Net

Polymorphism in C#.Net


C# C# 
Ratings:
0 Comment(s)
 4686  View(s)
Rate this:

Polymorphism in C#.Net

Polymorphism gives ability how my object works in different situation.Polymorphism means same operation may behave differently on different classes.Polymorphism means having more than one form. It allows you to invoke derived class methods through a base class reference during run-time.

Overloading and overriding are used to implement polymorphism.

Polymorphism is classified into compile time polymorphism or early binding or static binding and Runtime polymorphism or late binding or dynamic binding.

There are 6 way to achieve polymorphism:

1.       Operator overloading

2.       Method overloading

3.       Virtual function

4.       Generic  

5.       Interface

6.       Abstract class

Method overloading or Compile Time Polymorphism:

class task

    {

        public int show(int a)

        {

            return a;

        }

        public string show(string a)

        {

            return a;

        }

 

    }

    class Program

    {

        static void Main(string[] args)

        {

            task t = new task();

            Console.WriteLine(t.show(22));

            Console.WriteLine(t.show("Hello..."));

            Console.ReadLine();

        }

    }

In the example there is one function with two different arguments, one have integer and one have string.  So when a function perform different – different task with different – different argument that called function overloading.  

Method Overriding or Run Time Polymorphism:

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace abstarction

{

    public abstract class university

    {

        public abstract void BTech();

        public abstract void MBA();

    }

 

    public class GBTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("GBTU BTech Fee 50000/-");

        }

 

        public override void MBA()

        {

            Console.WriteLine("GBTU MBA Fee 100000/-");

        }

    }

 

    public class MTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("MTU BTech Fee 40000/-");

        }

 

        public override void MBA()

        {

            Console.WriteLine("MTU MBA Fee 800000/-");

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            GBTU g = new GBTU();

            g.BTech();

            g.MBA();

            MTU m = new MTU();

            m.BTech();

            m.MBA();

            Console.ReadLine();

        }

    }

}

 

Output-

GBTU BTech Fee 50000/-

GBTU MBA Fee 100000/-

MTU BTech Fee 40000/-

MTU MBA Fee 800000/-

Polymorphism via Generic:

 

First argument

class test<temp>

    {

        public temp show(temp t)

        {

            return t;

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            test<int> i = new test<int>();

            Console.WriteLine(i.show(22));

            test<string> s = new test<string>();

            Console.WriteLine(s.show("hello..."));

            Console.ReadLine();

        }

    }

 

Output:

22

Hello…

Second Argument

class test<temp>

    {

        public temp show(temp t)

        {

            return t;

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            test<int> i = new test<int>();

            Console.WriteLine(i.show(10));

            test<string> s = new test<string>();

            Console.WriteLine(s.show("How are you..."));

            Console.ReadLine();

        }

    }

 

Output:

10

How are you…

Note. Through generic object we can call generic and non generic function in generic class.


Don't want to miss updates? Please click the below button!

Follow MindStick