Polymorphism is an important concept in Object Oriented Programming. Polymorphism means one thing with potentially many forms. In programming, for example, if a same name method behaves differently in different situations, then method is called to be polymorphic. Method overloading and Method overriding are examples of method polymorphism. Method overloading is called Compile time polymorphism. Method overriding is called Runtime polymorphism.
If a method has the same name, then there must be some difference in it due to which there is a difference in its behavior. One of the reasons for the variation in the behavior of the method must be the variation in the type of parameters of the method. The parameter of a method is exactly the same as the input to a system. Just as the system gives a fixed output when input is entered into a system, similarly a method can be given zero or more inputs. The input to the method is called parameter.
Parameter Matter for Method Overloading
More number of parameters passed to method, the greater the load on the method because the method has to do some processing work for each parameter. That's why method overloading refers to the nature of parameters passed to the method which the method processes and returns a certain output. Method overloading refers not only to the number of parameters passed over the method but also to the nature of the parameter i.e. the data type of the parameter and the order of the parameters. The order in which the parameters are being entered within the method affects the method's output. Therefore, the variation in the type of the parameter of the method means the following type of variation.
- Number of parameters
- datatype of parameter
- Order of Parameters
We will understand these three types of Method Overloading with examples. A method named Method has been created in the following program class, which is overloaded with different parameters passed to the Method method.
- The first method is without any parameter.
- The second method has one parameter. of int datatype.
- The third method has one parameter. of string datatype.
- The fourth method has two parameters, int type parameter followed by string type parameter.
- The fifth method has two parameters, string type parameter followed by int type parameter.
Thus, even though they have same name, these methods behave differently due to parameters. This is an example of method overloading.
using System;
namespace MethodOverloads
{
class Program
{
void Method()
{
Console.WriteLine("Method 1: No parameter");
}
void Method(int i)
{
Console.WriteLine($"Method 2: One Parameter of int type {i}");
}
void Method(string s)
{
Console.WriteLine($"Method 3: One Parameter of string type: {s}");
}
void Method(int i, string s)
{
Console.WriteLine($"Method 4: Two Parameters; int type parameter followed by string type parameter: {i}, {s}");
}
void Method(string s, int i)
{
Console.WriteLine($"Method 5: Two Parameters; string type parameter followed by int type parameter: {s}, {i}");
}
static void Main(string[] args)
{
Console.WriteLine("Examples of method overloading:-");
Program p = new Program();
p.Method();
p.Method(100);
p.Method("Ajeet");
p.Method(200,"Amit");
p.Method("Amit",300);
Console.ReadLine();
}
}
}
Difference Between Method Overloading and Method Overriding
- Method overloading requires some kind of change in the parameter of the method, whether it is the number of parameters or the data type of the parameter or there is a difference in the order of the parameters, in these three ways method overloading of a method is possible but Method Overriding does not change the parameter of method. Method Overriding occurs due to change in the body of the method. In other words method overriding is due to change in the processing logic in the body of the method.
- Method Overriding happens in derived class but Method overloading is possible in parent class or derived class.
- Method overriding requires permission from the parent class, for this the virtual keyword is required along with methods within the parent class. If a virtual modifier is not used with a method of the parent class, that method cannot be overridden in the child. In contrast, method overloading in derived class does not require the permission of the parent class.
- Method overriding is always done within the inherited class. In this way method overriding is a part of inheritance whereas method overloading is not a part of inheritance. Method overloading can be done within the class or its child class.
- When the virtual keyword is used with a method within a parent class, that method can be overridden, but it is not necessary that the virtual method must be overridden. The virtual flag simply indicates that overriding of this method is possible within the child class.
- If a method is to be overridden in derived class, it is mandatory to use the virtual keyword with that method in the super class. Method overloading does not require the virtual keyword.
- The use of the override keyword override with a method indicates that a change has been made to the body of the method, that is, a method in a child class has changed from a method in its preceding parent class.
Example of Method Overriding
using System;
namespace Polymorphism2
{
abstract class Test
{
public abstract void Message();
}
class Parent
{
public void Display()
{
Console.WriteLine("Display Method without parameter inside same class.");
}
public void Display(string s)
{
Console.WriteLine("{0} Display Method is overloaded inside same class.", s);
}
public virtual void Show() // virtual => overridable
{
Console.WriteLine("Show method is overridable.");
}
}
class Child : Parent
{
public new void Display(string s)
{
Console.WriteLine("{0} Display Method is overloaded inside this Child class.", s);
}
public override void Show()
{
Console.WriteLine("Show method is overridden inside child class.");
}
}
class Test2 : Test
{
public override void Message() //abstract class mandatorily overridden to be consumed by inheriting class
{
Console.WriteLine("abstract class method overidden.");
}
}
}
using System;
namespace Polymorphism2
{
class Program
{
static void Main(string[] args)
{
Parent p = new Parent();
p.Display("Hello!");
p.Show();
Child c = new Child();
c.Display();
c.Display("Hi!");
c.Show();
Test2 t = new Test2();
t.Message();
Console.ReadLine();
}
}
}
Edited on 5th July 2023
No comments:
Post a Comment