Thursday, August 27, 2009

Polymorphism in C#

Introduction

Polymorphism allows to invoke derived class methods through a base class reference during run-time. This is useful when we need to assign a group of objects to an array and then invoke each of their methods. They won't necessarily have to be the same object type.

Features

1. It has the ability for classes to provide different implementations of methods that are called through the same name.
2. It allows you to invoke methods of derived class through base class reference during runtime.

Types of Polymorphism:

There are 2 types of Polymorphism. They are as follows.

1. Compile time polymorphism (or) Overloading
2. Runtime polymorphism (or) Overriding

Compile time polymorphism

Compile time polymorphism is method and operators overloading. It is also called early binding. Method with same name but with different arguments is called method overloading. In method overloading, method performs the different task at the different input parameters.

Example

class SillyMath
{
public static int Plus(int number1, int number2)
{
return Plus(number1, number2, 0);
}
public static int Plus(int number1, int number2, int number3)
{
return Plus(number1, number2, number3, 0);
}
public static int Plus(int number1, int number2, int number3, int number4)
{
return number1 + number2 + number3 + number4;
}
}

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.Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.

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.Method overloading has nothing to do with inheritance or virtual methods.

Example:

namespace MethodOverriding
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}
class Program
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output
Console.ReadLine();
}
}
}

Advantages

* Polymorphism is one of the features provided by OOP. Polymorphism makes it possible to call different mathods using one interface.
* A language can implement polymorphism in various forms. First is method overloading and the second is method overridding.This is implemented through *new* keyword in C#.

Disadvantages

* It's esoteric. Not very easy for the beginner to just pick up and go with it. Rather it takes often years of dedication before abstraction becomes second nature.
* Huge up-front hit in migrating legacy code as it often has to re-designed from scratch. Of course in the long run, in terms of re-use, performance and robustness, this eventually becomes an advantage..

Polymorphism with interfaces

Polymorphism is posiible with interfaces. A sample program that shows how to achieve polymorphism with interfaces.

In the Main method the respective overriden CustomerType() method is called.

Example:

interface Customer
{
void CustomerType();
}
public class CorporateCustomer : Customer
{
public void CustomerType()
{
Console.WriteLine("I am a corporate customer");
}
}
public class PersonalCustomer : Customer
{
public void CustomerType()
{
Console.WriteLine("I am a personal customer");
}
}
public class MainClass
{
public static void Main()
{
Customer[] C = new Customer[2];
C[0] = new CorporateCustomer();
C[1] = new PersonalCustomer();
foreach (Customer CustomerObject in C)
{
CustomerObject.CustomerType();
}
}
}

Summary:

In this article, we have seen different Polymorphism features in C#.

Tuesday, July 14, 2009

Google Developed New OS !

See
Great News For Every One !
Google Developed new Operating System Called Chrome OS by Google.

For All Informations and Snapshots- Click Here



Regards
Dinesh Verma