Method overloading is a way to achieve compile time Polymorphism where we can use a method with the same name but different signature, Method overloading is done at compile time and we have multiple way to do that but in all way method name should be same.
- Number of parameter can be different.
- Types of parameter can be different.
- Order of parameters can be different.
Example:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Hello_Word {
- class overloding {
- public static void Main() {
- Console.WriteLine(volume(10));
- Console.WriteLine(volume(2.5F, 8));
- Console.WriteLine(volume(100L, 75, 15));
- Console.ReadLine();
- }
- static int volume(int x) {
- return (x * x * x);
- }
- static double volume(float r, int h) {
- return (3.14 * r * r * h);
- }
- static long volume(long l, int b, int h) {
- return (l * b * h);
- }
- }
- }
If we have a method that have two parameter object type and have a same name method with two integer parameter so when we call that method with int value so it’ll call that method have integer parameter instead of object type parameters method.
No comments:
Post a Comment