Wednesday 19 August 2015

What is Overloading Method in C#.Net

Overloading :
                       This allows to define multiple methods in a class with the same name by changing their signatures.
    Overloading is the two types 
  1.  Method Overloading.
  2. Constructor Overloading.
Method Overloading :
                                  Defining of multiple methods in a class using the same name by changing their signature(only input parameters) is known as method Overloading.
                                  Changing the Signature in the sense we can either change the number of parameters being passed to the method  (OR)  Type of parameter pass to method (OR)  Order of parameter being passed to the Method.

Eg :  public void show();
        public void show(int x);
        public void show(int x, int y);
        public void show(int x, string y);
        public void show(string s);
        public  void show (char c);  and soon.....

Constructor Overloading :
                          Just like we can overload methods in the same way we can overload constructors also. It is we can define multiple constructor in a class.

Example : Add a class Loadcon.cs and write the following .
     
                   Using System;
                 class Loadcon
                    {
                         int x ;
                    public Loadcon()
                       {
                        x = 10;
                       }
                       public Loadcon(int x)
                       {
                        this.x = x;
                       }
                      public void display()
                       {
                       Console.WriteLine(x);
                        }
                        Static void Main()
                        {
                             Loadcon c1 = new Loadcon();
                             Loadcon c2 = new Loadcon();
                             c1.display();
                              c2.display();
                             Console.ReadLine();
                             }
                         }

  Note : 
               When a class has multiple constructor in it. We can make use of any available constructor for creating the object of class and invoke it's members.
                 Constructor Overloading allows to initialize the variables of a class either by using Default values or Initialize by sending our own values.In the above example the variable 'x' is initialize with default value 10. When object is created using default or initialize with the value 50.We can send using parameterized constructor.


Inheritance Based Overloading :
                              We can Overload a method either within a class or within it's child classes  also. If a method is Overload under the child class it's known as Inheritance based Overloading.


Example :  LoadParent.cs
            
                Using System;
                  class LoadParent
                   {
                     public void test()
                        {
                             Console.WriteLine("method1");
                            }
                        public void test(int x)
                        {
                             Console.WriteLine("method2");
                            }
       
Example1 : LoadChild.cs


                   using System;
   
                  class LoadChild : LoadParent
                   {
                     public void test(string s)
                        {
                             Console.WriteLine("method2");
                            }
                         Static void Main()
                       {
                         LoadChild c = new LoadChild();
                                    c.test();
                                    c.test(10);
                                    c.test("hello");
                          Console.ReadLine();
                        }
                   }


OutPut :     method1
                   method2
                   method3

No comments:

Post a Comment