Author - StudySection Post Views - 694 views
overloading

Method Overloading Case studies

Method Overloading:

The basic purpose of method overloading is to operate a single functionality on a different type of object.

For example, the operation “convert numbers to a required standard type”, for instance, convert ‘byte to double’, ‘short to double’, and so on, and don’t forget to add signed integers into the list.

To implement the above functionality, it is more efficient to have a single method named ConvertToStandard or ConvertToDouble , instead of having different names for each conversion type, for instance, ConvertByteToDouble , ConvertSByteToDouble .

Below are some examples of method overloading. Try to answer yourself and then verify with the given one.

Case 1: We have a basic code example. Class Test has 2 methods with the name m1, they are overloaded methods. Within the “Main” method of the class Program, we will call overloaded methods bypassing different types of arguments and see what outputs we will get.
class Test
{
public void m1(int i)
{
Console.WriteLine("int-arg method");
}
public void m1(float f)
{
Console.WriteLine("float-arg method");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1(1);
t.m1(10.5f);
t.m1('a');
t.m1(10L);
t.m1(10.5);
}
}

Inside the Main method, we create an object of the test class and we call m1 methods for different types of parameters.

Output :
t.m1(1) : int-arg method
t.m1(10.5f) : float-arg method
t.m1(‘a’) : int-arg method

There is a phenomenon called automatic type promotion, it is handled by the compiler. Because of the automatic promotion of data types, the char type is automatically converted to int.

The below-attached diagram shows the automatic type promotion flow. Arrow’s direction describes that promotion flow cannot be reversed, to put it another way, data type on the right side cannot be down promoted to its left ones.
chart
t.m1(10L) : float-arg method
Long-type variables are promoted to float automatically.

t.m1(10.5): CE: cannot find symbol method m1(double). Location: class Test.
A double type variable cannot be down promoted to float or int.

Case 2: in this example, we have almost the same setup as in the previous one.
class Test
{
public void m1(string s)
{
Console.WriteLine("string-arg method");
}
public void m1(object o)
{
Console.WriteLine("object-arg method");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1(new object());
t.m1("Test");
t.m1(null);
}
}

Inside the Main method, we called m1 passing a new object, a string, and a null.

Output :
t.m1(new object()) : object-arg method
t.m1(“Test”) : string-arg method
t.m1(null) : string-arg method

The process of the compiler to resolve a method call from given overloaded method definitions or to connect a method call to one of the given definitions is called overloaded method resolution.

While resolving the overloaded method, if the compiler cannot find the exact match then it will always give precedence or priority to the child type argument than parent type.

This is the reason we got the “string-arg method” as output when we passed null as an argument to the method. String and object both can have null values, but the compiler tends towards the method with child arguments. Let’s see another example to understand this.

class Test
{
public void m1(int i)
{
Console.WriteLine("int-arg method");
}
public void m1(long i)
{
Console.WriteLine("long-arg method");
}
public void m1(float i)
{
Console.WriteLine("float-arg method");
}
public void m1(double d)
{
Console.WriteLine("double-arg method");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1(0);
}
}

Inside the Main method, we have passed “0”{zero} as an argument. Now, the compiler resolution will call the method with a child argument close to the passed argument.

Output:
t.m1(0) : int-arg method

Case 3: now, we will discuss, what if available method definitions are at the same level of hierarchy.

class Test
{
public void m1(string s)
{
Console.WriteLine("string-arg method");
}
public void m1(StringBuilder stringBuilder)
{
Console.WriteLine("StringBuilder-arg method");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1("Test");
t.m1(new StringBuilder("Test"));
t.m1(null);
}
}

chart1

Both string and StringBuilder can have a null value, and they both are at the same level of the inheritance, so the compiler has equal precedence for both of them.

Output:
t.m1(“Test”): string-arg method
t.m1(new StringBuilder(“Test”)): StringBuilder-arg method
t.m1(null): CE : The call is ambiguous between the following methods or properties: ‘Test.m1(string)’ and ‘Test.m1(StringBuilder)

Due to equal precedence for both data types, the compiler cannot resolve any method that matches the 3rd test case “t.m1(null)”. We get the compile time error.

Case 4: In this case, we will understand if the order in which parameters are passed does matter at all while considering the overloading concept.

class Test
{
public void m1(int i, float f)
{
Console.WriteLine("int-float version");
}
public void m1(float f, int i)
{
Console.WriteLine("float-int version");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1(10, 10.5f);
t.m1(10.5f, 10);
t.m1(10, 10);
t.m1(10.5f, 10.5f);
}
}

Output:
t.m1(10, 10.5f): int-float version
t.m1(10.5f, 10): float-int version
t.m1(10, 10): CE: The call is ambiguous between the following methods or properties: ‘Test.m1(int, float)’ and ‘Test.m1(float, int)’

Due to implicit conversion or automatic type promotion, the compiler is not able to call one form of an overloaded method.

t.m1(10.5f, 10.5f) : CE: cannot find symbol. Symbol : method m1(float, float). Location : class Test.

Case 5: In this case, var-arg method overloading is discussed.
class Test
{
public void m1(int x)
{
Console.WriteLine("General method");
}
public void m1(params int[] x)
{
Console.WriteLine("float-int version");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.m1();
t.m1(10);
t.m1(10, 20);
}
}

Output:
t.m1(): var-arg method
var-arg method argument array length can be zero.
t.m1(10): General method
t.m1(10,20): var-arg method

Case 6: This is the last example in the article, we will create multiple classes and their objects. In this case, we will illustrate why overloading is also known as early-binding, static-polymorphism, compile-time polymorphism.
class Animal
{
//Animal Class
}
class Monkey: Animal
{
//Monkey Class
}
class Test
{
public void m1(Animal a)
{
Console.WriteLine("Animal");
}

public void m1(Monkey m)
{
Console.WriteLine("Monkey");
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
Animal a = new Animal();
t.m1(a);
Monkey m = new Monkey();
t.m1(m);
Animal a1 = new Monkey();
t.m1(a1);
}
}

Output:
t.m1(a): Animal
t.m1(m): Monkey
t.m1(a1) : Animal

Because parent class objects can easily hold their child class object, in the 3rd output we are creating an object of type Monkey and assigning it to Animal class object.

The compiler is responsible for method resolution while method overloading. So at compile time, the compiler will check the type of the object and bind its implementation method.

In our case, at compile time a1 is of the Animal type consequently, the “m1(Animal a)” method will be executed. This method resolution at compiler time is designated as early-binding, static-polymorphism, compile-time polymorphism.

StudySection has a long list of certification exams that it offers through its online platform. The PHP Certification Exam is one of the programming certifications that it provides. Whether you are new to PHP programming or you have extensive experience in PHP programming, you can get a certification according to your level. Attach a PHP certification with your resume to get the most out of job offers.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri