Polymorphism

Polymorphism is a Greek word that means multiple forms or shapes. You can use polymorphism if you want to have multiple forms of one or more methods of a class with the same name.

In C#, polymorphism can be achieved in two ways:

  1. Compile-time Polymorphism
  2. Run-time Polymorphism

Compile-time Polymorphism (Method Overloading)

Compile-time polymorphism is also known as method overloading. C# allows us to define more than one method with the same name but with different signatures. This is called method overloading.

Method overloading is also known as early binding or static binding because which method to call is decided at compile time, early than the runtime.

Rules for Method Overloading:

  1. Method names should be the same but method signatures must be different. Either the number of parameters, type of parameters, or order of parameters must be different.
  2. The return type of the methods does not play any role in the method overloading.
  3. Optional Parameters take precedence over implicit type conversion when deciding which method definition to bind.

The following example demonstrates the method overloading by defining multiple Print() methods with a different number of parameters of the same type.

Example: Method Overloading Method Overloading
class ConsolePrinter
{
    public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(string str1, string str2){ 
        Console.WriteLine($"{str1}, {str2}");
    }

    public void Print(string str1, string str2, string str3){ 
        Console.WriteLine($"{str1}, {str2}, {str3}");
    }
}

The following example demonstrates polymorphism using the same number of parameters with different types.

Example: Method Overloading
class ConsolePrinter
{
    public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(int a){ 
        Console.WriteLine($"Integer {a}");
    }
}

The following example demonstrates polymorphism using the same number of parameters with different sequences.

Example: Method Overloading
class ConsolePrinter
{
    public void Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
    }

    public void Print(string str, int a){ 
        Console.WriteLine($"{a}, {str}");
    }
}

Please note that return type of methods is not considered. The following will give a compile-time error.

Example: Invalid Method Overloading
class ConsolePrinter
{
    public void Print(int a, string str)
    { 
        Console.WriteLine($"{a}, {str}");
    }

    public int Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
        return 0;
    } 
}

The following methods overloaded with different types, sequence, and number of parameters.

Example: Method Overloading
class ConsolePrinter
{
     public void Print(string str){ 
        Console.WriteLine(str);
    }

    public void Print(string str1, string str2){ 
        Console.WriteLine($"{str1}, {str2}");
    }

    public void Print(string str1, string str2, string str3){ 
        Console.WriteLine($"{str1}, {str2}, {str3}");
    }
	
    public void Print(int a){ 
        Console.WriteLine($"Integer {a}");
    }
	
	 public void Print(int a, string str){ 
        Console.WriteLine($"{a}, {str}");
    }

    public void Print(string str, int a){ 
        Console.WriteLine($"{a}, {str}");
    }
}

Invoking Overloaded Methods

We can call the overloaded method by passing the exact parameter it requires. For example, if we want to invoke the print(string str) method that displays a string value, we will pass only one argument of string type. Likewise, if we want to invoke the Print(int a, string str) method, we will pass int and string type argument.

Example: Method Overloading
public static void Main()
{
	ConsolePrinter cp = new ConsolePrinter();
	cp.Print("Hello World!");
	cp.Print(1, "John");
}

Thus, polymorphism using method overloading plays important role in designing an application. The simplest example in .NET API is the overloads of Console.WriteLine() methods.

Learn about runtime polymorphism next.