Advertisement
//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);
}
}
The sum of 1 and 2 by Add() method: 3
The sum of 2.5 and 4.7 by Add() method: 7.2
//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));
}
}
The sum of 1 and 2 by Add() method: 3
The sum of 2.5 and 4.7 by Add() method: 7.2
//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");
}
}
Rabbit eats carrots
Rabbit eats grass
Rabbit eats fruits and vegetables
Advertisement
//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");
}
}
Play any sport
Play Tennis
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement