When
a message can be processed in different ways is called polymorphism.
Polymorphism means many forms.
Polymorphism is of two types:
- Compile time polymorphism/Overloading
- Runtime polymorphism/Overriding
Compile Time Polymorphism
Compile
time polymorphism is method and operators overloading. It is also called early
binding.
In
method overloading method performs the different task at the different input
parameters.
Runtime Time
Polymorphism
Runtime
time polymorphism is done using inheritance and virtual functions. Method
overriding is called runtime polymorphism. It is also called late binding.
When
overriding a method, you change the behavior of the method for the
derived class. Overloading a method simply involves having another
method with the same prototype.
Caution: Don't confused method
overloading with method overriding, they are different, unrelated concepts. But
they sound similar.
Practical example of Method Overloading
(Compile Time Polymorphism)
using
System;
namespace
method_overloading
{
class Program
{
public class Print
{
public void
display(string name)
{
Console.WriteLine ("Your
name is : " + name);
}
public void
display(int age, float
marks)
{
Console.WriteLine ("Your
age is : " + age);
Console.WriteLine ("Your
marks are :" + marks);
}
}
static void
Main(string[] args)
{
Print obj = new
Print ();
obj.display ("George");
obj.display (34, 76.50f);
Console.ReadLine ();
}
}
}
When and why to
use method overloading
Use
method overloading in situation where you want a class to be able to do
something, but there is more than one possibility for what information is
supplied to the method that carries out the task.
You
should consider overloading a method when you for some reason need a couple of
methods that take different parameters, but conceptually do the same thing.
Things to keep in mind while method overloading
If
you use overload for method, there are couple of restrictions that the compiler
imposes.
The
rule is that overloads must be different in their signature, which means the
name and the number and type of parameters.
Method Overriding:
Base class method has to be marked with virtual keyword and we can override it in derived class using override keyword.
Derived class method will completely overrides base class method i.e. when we refer base class object created by casting derived class object a method in derived class will be called.
Example:
// Base class
public class BaseClass
{
public virtual void Method1()
{
Console.Write("Base Class Method");
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived Class Method");
}
}
// Using base and derived class
public class Sample
{
public void TestMethod()
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
Output
---------------------
Derived Class Method
Derived Class Method---------------------
No comments:
Post a Comment