Friday 22 February 2013

Polymorphism

Its an approach of definning different methods with a different behaviour i.e whenever the input changes the output or behaviour also changes accordingly which can be implemented under a language using different approaches like Overloading, Overriding and Hiding.

Overloading
This is again of three types:
  • Method Overloading
  • Constructor Overloadding
  • Operator Overloading 
Method Overloading: It is an approach of definnig multiple methods under a class with the same name, by changing the signature of those methods. Changing the signature of those method in the sense we can change the number of parameters being passed to the method or order of parameters being passed to method.

//Method overloading

using System;

namespace CSharpConsole
{
    class LoadDemo
    {
        public void Show()
        {
            Console.WriteLine(1);
        }
        public void Show( int x)
        {
            Console.WriteLine(2);
        }
        public void Show(string s)
        {
            Console.WriteLine(3);
        }
        public void Show(int x, string s)
        {
            Console.WriteLine(4);
        }
        public void Show(string s, int x)
        {
            Console.WriteLine(5);
        }

        static  void Main()
        {
            LoadDemo d = new LoadDemo();
            d.Show();
            d.Show(10);
            d.Show("hello");
            d.Show(5,"leena");
            d.Show("bansiwal", 15);
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment