Saturday, 20 December 2014

Polymorphism and its types with examples


 Polymorphism and its types

 

Polymorphism

Polymorphism means many forms. It is the ability to take more then one form. Poly means "multiple" and morph means "forms" so polymorphism means many forms.

In polymorphism

we will declare methods with same name and different parameters in the same class or

OR

we will declare methods with same name and same parameters in different classes.

Types of Polymorphism

  1. Compile Time Polymorphism (Early Binding or Overloading or static binding)
  2. Run Time Polymorphism(Late Binding or Overriding or dynamic binding)


Compile Time Polymorphism

  • In this Polymorphism we declare functions with same name but with different signatures(different params).
  • By using this type of polymorphism we can perform different tasks with same method name.
  • It is also called as early binding or function overloading or static binding(static polymorphism)

Overloading can be done by the following four ways:

  • Have different number of parameter.
  • Having same number of parameters but of different type. 
  • Having same number and type of parameters but in different orders. 
  • Having same number of parameters with  same type but parameter with "out" or "ref" keyword in one function. 

 A method cannot be overloaded on the basis of

  •  Different return type
  •  Different access modifier
  •  Normal and optional parameters

Example:

public class Class1
{
public void NumbersAdd( int a, int b)
{
Console .WriteLine(a + b);
}
public void NumbersAdd( int a, int b, int c)
{
Console .WriteLine(a + b + c);
}
}


Live Example of Overloading :

  If we find a customer and then we can find a customer by  Customerid,  customerName , customerName and City etc.

public List<Customer> FindCustomer(String customer_name){ // your code}
public List<Customer> FindCustomer(Int32 customer_Id){ // your code}
public List<Customer> FindCustomer(String customer_name, String city){  // your code}


Run Time Polymorphism

  • In this Polymorphism we declare functions with same names with same signatures.
  • In this we can override a method of base class by creating similar function in derived class. This is achieved by using inheritance and "virtual" & "override" keywords.
  • It is also called as late binding or function overriding or dynamic binding(dynamic polymorphism).
  • In base class if we declare method with virtual keyword then only we can override those methods in derived class using override keyword.


Example:

//Base Class
 public class Bclass
{
public virtual void Sample1()
{
Console .WriteLine( "Base Class" );
}
}

// Derived Class
public class DClass : Bclass
{
public override void Sample1()
{
Console .WriteLine( "Derived Class" );
}
}

// Using base and derived class
class Program
{
static void Main( string [] args)
{
// calling the overriden method
DClass objDc = new DClass ();
objDc.Sample1();
// calling the base class method
Bclass objBc = new DClass ();
objBc.Sample1();
}
}

Output

Derived Class
Derived Class



Live example of overriding:
  • You're using 3rd party library Foo, which contains a class FooBase
  • You write your own subclass of Foo, Bar
  • You introduce a public method in Bar called DoSomething
  • In the next version of Foo, the 3rd party introduces a DoSomething method in Foo which doesn't do the same as your method. This may or may not be virtual.
&nbsp

1 comment:

  1. Hi Guys,

    If you have any questions regarding the blog please write to me.

    ReplyDelete