Advertisement



< Prev
Next >



C# Method Overloading





In general, method overloading is creating one or more methods with the same name as an existing method in a class. In other words, you can even say that the method overloading allows us to have multiple versions of a method within a class.




Why method overloading is used?


Method overloading is mainly used in a situation when we want to create multiple specialized versions of a method in a class, with each version having the same name and doing some specific specialized task.




How method overloading is achieved?







    Rules to method overloading -


    • Overloaded methods MUST have different types or number of arguments.
    • Overloaded methods may/may not have different return types.
    • Overloaded methods may/may not have different access modifiers.



    1. Overloaded methods MUST have different types or number of arguments.

    2. This is the most important rule to perform the method overloading. To create multiple versions of a method, each version of this method MUST have different types or number of arguments.

      //C# Example of method overloading
      
      
      using System;
      
      class Math
      {
      //Defining one version of Add() method of Math class
      //This method takes two int values
      public void add(int a, int b)		//Add method adding two integers
      {
      	Console.Write("The sum of {0} and {1} by Add() method: ",a , b);
      	Console.WriteLine(a+b);
      }
      
      //Defining another overloaded version of Add() method of Math class
      //This method takes two float values
      public void add(float a, float b)	//Add method adding two floats
      {
      	Console.Write("The sum of {0} and {1} by Add() method: ",a , b);
      	Console.WriteLine(a+b);
      }
      }
      
      
      class Overload
      {
      public static void Main(String[] ar)
      {
      	//Creating an object of Math class
      	Math ob = new Math();
      	
      	//Calling the Add() version method of Math class
      	//which takes two int values
      	ob.add(1,2);
      
      	//Calling the Add() version of Math class
      	//Which takes two float values
      	ob.add(2.5f,4.7f);
      }
      }
      

      Output -

      The sum of 1 and 2 by Add() method: 3
      The sum of 2.5 and 4.7 by Add() method: 7.2
      • Within the Math class, we have overloaded its Add() method, thereby creating its two different versions within its Math class.
      • One version of the Add() method is adding two int type values.
      • While, the second version of the Add() method is adding two float type values.




    3. Overloaded methods may/may not have different return types.

    4. In the previous coding example, you have seen the overloaded versions of Add() method having a similar, void return type. In this example, each overloaded version of Add() method will have a different return type but it wouldn't make any difference to the process of method overloading.

      //C# Example of method overloading
      //Overloaded methods may/may not have different return types
      
      
      using System;
      
      class Math
      {
      //Defining a version of Add() method of Math class
      //This method takes two int values and returns int value
      public int add(int a, int b)		//Add method adding two integers
      {
      	Console.Write("The sum of {0} and {1} by Add() method: ",a , b);
      	return(a+b);
      }
      
      //Defining another overloaded version of Add() method of Math class
      //This method takes two float values and returns float value
      public float add(float a, float b)	//Add method adding two floats
      {
      	Console.Write("The sum of {0} and {1} by Add() method: ",a , b);
      	return(a+b);
      }
      }
      
      
      class Overload
      {
      public static void Main(String[] ar)
      {
      	//Creating an object of Math class
      	Math ob = new Math();
      	
      	//Calling the Add() version method of Math class
      	//which takes two int values
      	Console.WriteLine(ob.add(1,2));
      
      	//Calling the Add() version of Math class
      	//Which takes two float values
      	Console.WriteLine(ob.add(2.5f,4.7f));
      }
      }


      Output -


      The sum of 1 and 2 by Add() method: 3
      The sum of 2.5 and 4.7 by Add() method: 7.2





    5. Overloaded methods may/may not have different access modifiers.

    6. In the previous coding example, you have seen the overloaded versions of Add() method having a similar, public access modifier. In this example, we will overload the Eat() method of Rabbit class, where each overloaded version will have a different access modifier.

      //C# Example of method overloading
      //Overloaded methods may/may not have different access modifiers.
      
      using System;
      
      class Rabbit
      {
      //A version of Eat() method of Rabbit class
      //with protected access modifier
      protected void Eat() 	     //eat method with protected access modifier
      {
      	Console.WriteLine("Rabbit eats carrots");
      }
      
      
      //An overloaded version of Eat() method of Rabbit class
      //with public access modifier
      public void Eat(String st)  
      {
      	Console.WriteLine("Rabbit eats "+ st);
      }
      
      //Another overloaded version of Eat() method of Rabbit class
      //with private access modifier
      public void Eat(String st1, String st2)   
      {
      	Console.WriteLine("Rabbit eats "+ st1 + " and " + st2);
      }
      
      //Defining Main method of Rabbit class
      public static void Main(String[] ar)
      {
      	//Creating an object of Rabbit class
      	Rabbit ob= new Rabbit();
      
      	//Calling an overloaded version of Eat() method
      	//Which doesn't take any value
      	ob.Eat();
      
      	//Calling an overloaded version of Eat() method
      	//Which takes a String value
      	ob.Eat("grass");
      
      
      	//Calling an overloaded version of Eat() method
      	//Which takes a String value
      	ob.Eat("fruits", "vegetables");
      }
      }


      Output -


      Rabbit eats carrots
      Rabbit eats grass
      Rabbit eats fruits and vegetables







    Advertisement




    Overloading through inheritance


    In all the previous coding examples we saw how to create multiple versions of a method within the same class but now we will see how to create multiple versions of a method which is inherited by a class(subclass) from another class(superclass).

    //C# Example of method overloading by inheritance
    
    using System;
    
    //Defining a superclass
    class Sports			
    {
    //The original Play() method with no arguments
    public void Play()		
    {
    	Console.WriteLine("Play any sport");
    }
    }
    
    
    //Defining a Tennis class, which is subclass of Sports class.
    class Tennis:Sports	
    {
    //An overloaded Play() method of Tennis class, with String arguments
    public void Play(String name)	
    {
    	Console.WriteLine("Play "+ name);
    }
    }
    
    class Overload 
    {
    public static void Main(String[] ar)
    {
    	//Creating an object of Tennis class.
    	Tennis ob = new Tennis();
    
    	//Calling a version of the Play() method of Tennis class
    	//Which was inherited by Sports class
    	//And, takes no arguments.
    	ob.Play();
    
    	//Calling a version of the Play() method of Tennis class
    	//Which takes a String argument.
    	ob.Play("Tennis");
    }
    }


    Output -


    Play any sport
    Play Tennis


    Program Analysis


    • In this code, we have a class Sports, which is inherited by another class Tennis and the inherited method, Play() of Sports class is overloaded in the Tennis class.
    • While, the Play() method in class Sports was displaying a general message "Play any sport", the overloaded method Play() in its subclass, Tennis, is displaying a specialized message "Play Tennis".




    Please share this article -




    < Prev
    Next >
    < C# Destructors
    C# Method Overriding >



    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement