Polymorphism Explained

Polymorphism is the ability of an object to represent more than one type.

The MSDN Library explains polymorphism quite well. Emphasis mine.

C#:
  1. public class A
  2. {
  3.     // code
  4. }
  5.  
  6. public class B : A
  7. {
  8.     // code
  9. }

"In the example above, class B is effectively both B and A. When you access a B object, you can use the cast operation to convert it to an A object. The B object is not changed by the cast, but your view of the B object becomes restricted to A's data and behaviors. After casting a B to an A, that A can be cast back to a B. Not all instances of A can be cast to B—just those that are actually instances of B."

Update 19 February 2008

Polymorphism enables a client application to use instances of derived classes interchangeably, without having to determine exactly what type of instance it is dealing with.

The client application can create an instance of a derived class and assign it to a variable of the base class type. If the client application invokes an overridable member through the base class variable, polymorphism automatically ensures that the correct version of the member is invoked; the client application does not need to test each instance to determine its precise class type.

Example

A BankAccount class might define overrideable methods such as Deposit and Withdraw, and overridable properties such as AvailableFunds and InterestPayable. Derived classes such as SavingsAccount and CheckingAccount can override some or all of the overridable members, as appropriate.

The following code shows how you can create SavingsAccount and CheckingAccount instances, assign them to BankAccount variables, and then invoke the Deposit method on each instance. The first variable refers to a SavingsAccount object, so the SavingsAccount version of the Deposit method will be invoked in that case. The second variable refers to a CheckingAccount object, so the CheckingAccount version of the Deposit method will be invoked in that case.

C#:
  1. BankAccount b1 = new SavingsAccount();
  2. b1.Deposit();   // invokes SavingsAccount's Deposit
  3.  
  4. BankAccount b2 = new CheckingAccount();
  5. b2.Deposit();   // invokes CheckingAccount's Deposit

Note: If the client application invokes an overridable member on a derived class instance, but the derived class does not override that member, the base class version of the member will be called instead. For example, if the client application invokes the Deposit method on a SavingsAccount instance, but the SavingsAccount class does not override the Deposit method, the BankAccount version of the Deposit method will be called.

(From Microsoft Official Course 4995A: Programming with the Microsoft .NET Framework Using Microsoft Visual Studio 2005, p. 5-10)

Update 14 August 2008

Polymorphism is where an object has the properties and methods of the declared type, but implements the overridden properties and methods of the instantiated type.

C#:
  1. BaseClass b = new DerivedClass();

5 June 2007 | .NET, Software engineering, C# | Comments

Comments:

  1.  
  2.  
  3.