Vous êtes sur la page 1sur 5

There are two types of polymorphism:

1.
2.

Static or compile time polymorphism


Dynamic or runtime polymorphism

Static or Compile Time Polymorphism


In static polymorphism, the decision is made at compile time.

Which method is to be called is decided at compile-time only.


Method overloading is an example of this.
Compile time polymorphism is method overloading, where the compiler knows which
overloaded method it is going to call.

Method overloading is a concept where a class can have more than one method with the same
name and different parameters.

Method Overloading Example:


class Program
{
public static void Main()
{
ShowString();
ShowString("Category");
}
static void ShowString()
{
// Send default argument to overload.
ShowString("Popular");
}

static void ShowString(string value)


{
// We don't need an if check here, which makes
// ... calling this method directly faster.
Console.WriteLine(value);
}
}

Operator Overloading
You can redefine or overload most of the built-in operators available in C#. Thus a
programmer can use operators with user-defined types as well. Overloaded operators are
functions with special names the keyword operator followed by the symbol for the operator
being defined.
Operators

Description

+, -, !, ~, ++, --

These unary operators take one operand and


can be overloaded.

+, -, *, /, %

These binary operators take one operand and


can be overloaded.

==, !=, <, >, <=, >=

The comparison operators can be overloaded

&&, ||

The conditional logical operators cannot be


overloaded directly.

+=, -=, *=, /=, %=

The assignment operators cannot be


overloaded.

=, ., ?:, ->, new, is, sizeof, typeof

These operators cannot be overloaded.

An unary operator is an operator that operates on a single "argument"; like unary minus to
negate a number (-a), ++ to pre-post-increment a number ( a++) and so on...
A binary operator is an operator taking two arguments, like * (a*b), - (a-b)

class Widget
{
public int _value;
public static Widget operator +(Widget a, Widget b)
{
// Add two Widgets together.
// ... Add the two int values and return a new Widget.
Widget widget = new Widget();
widget._value = a._value + b._value;
return widget;
}
public static Widget operator ++(Widget w)
{
// Increment this widget.
w._value++;
return w;
}
}
class Program
{
static void Main()
{
// Increment widget twice.
Widget w = new Widget();
w++;
Console.WriteLine(w._value);
w++;
Console.WriteLine(w._value);
// Create another widget.
Widget g = new Widget();
g++;
Console.WriteLine(g._value);
// Add two widgets.
Widget t = w + g;
Console.WriteLine(t._value);
}
}

Dynamic or Runtime Polymorphism


Run-time polymorphism is achieved by method overriding.
Method overriding allows us to have methods in the base and derived classes with the same
name and the same parameters.
By runtime polymorphism, we can point to any derived class from the object of the base class at
runtime that shows the ability of runtime binding.
Through the reference variable of a base class, the determination of the method to be called is
based on the object being referred to by reference variable.

If override is not used, the base class method will be


called. If Override is used, the derived class method will be
called.
Overriding example:
namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
}
public class Derived : Base
{
public override void Show()
{
Console.WriteLine("Show From Derived Class.");
}
}
static void Main(string[] args)
{
Base objBase;
objBase = new Base();
objBase.Show();// Output ----> Show From Base Class.
objBase = new Derived();
objBase.Show();//Output--> Show From Derived Class.
Derived derived = new Derived();
derived.Show();//Output--> Show From Derived Class.
Console.ReadLine();
}
}
}

Without Override example:


namespace PolymorphismByManishAgrahari
{
class Program
{
public class Base
{
public virtual void Show()
{
Console.WriteLine("Show From Base Class.");
}
}
public class Derived : Base
{
Public void Show()

{
Console.WriteLine("Show From Derived Class.");
}
}

static void Main(string[] args)


{
Derived derived = new Derived();
derived.Show();//Output ----> Show From Base Class.
Console.ReadLine();
}
}
}