Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Polymorphism

In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
In programming languages,polymorphism is to provide a single interface to different types of entities. A variable with a given name may be allowed to have different forms and the program can determine which form of the variable to use at the time of execution.
For example, a person let's say John can drive a bicycle, a scooter, a bike and a car. Now, depending on availability of vehicle or weather condition John can choose the vehicle to go somewhere. If the person knows only cycling then he has to choose only bicycle to go anywhere.The same we can relate in programing.

Types Of Polymorphism-
1-Compile Type Polymorphism.
    A-Operator Overloading
    B-Function Overloading
2-Run Time Polymorphism.
    A-Virtual Function

Operator Overloading-
In an object oriented programming,Operator overloading is the process of giving different behavior to an operator. Operator overloading permits user-defined operator implementations to be specified for operations where one or both of the operands are of a user-defined class or struct type.
for ex. '.' represnt decimal but I want to to use '.' to add two no ,by overloading I can use '.' to perform addition of two no.
operator overloading provides a much more natural way of implementing the operations on custom types. Suppose that we have a class created for Complex number and we want to perform all the arithmetic operations on this type. One way to do this is by having functions like Add, Subtract inside the class and have the functionality. Another way is to actually have the overloaded version of operators to act on this type.
Example 1
This example shows how you can use operator overloading to create a complex number class Complex that defines complex addition. The program displays the imaginary and the real parts of the numbers and the addition result using an override of the ToString method.
// complex.cs

using System;
public struct Complex
{
     public int real;
    public int imaginary;
    public Complex(int real, int imaginary)
    {
      this.real = real;
      this.imaginary = imaginary;
    }
    // Declare which operator to overload (+), the types
    // that can be added (two Complex objects), and the
    // return type (Complex):
    public static Complex operator +(Complex c1, Complex c2)
    {
      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }
    // Override the ToString method to display an complex number in the suitable format:
    public override string ToString()
    {
      return(String.Format("{0} + {1}i", real, imaginary));
    }
    public static void Main()
    {
      Complex num1 = new Complex(2,3);
      Complex num2 = new Complex(3,4);
      // Add two Complex objects (num1 and num2) through the
      // overloaded plus operator:
      Complex sum = num1 + num2;
      // Print the numbers and the sum using the overriden ToString method:
      Console.WriteLine("First complex number: {0}",num1);
      Console.WriteLine("Second complex number: {0}",num2);
      Console.WriteLine("The sum of the two numbers: {0}",sum);
    }
}
Output
First complex number: 2 + 3i
Second complex number: 3 + 4i
The sum of the two numbers: 5 + 7i


Function Overloading-
In some programming languages, Function overloading or method overloading is the ability to create multiple methods of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
For example, Add(int a , int b) and Add(int a, int b, int c) are overloaded methods.
If we want to call first method then we have to pass two parameter in add method and if we want to call second method then we have to pass three parameter in Add method.

Function can be overloaded in following ways.
1-By passing the no of arguments to a function.
Add(int a , int b) and Add(int a, int b, int a)
2-By passing type of argument.
Add(int a , int b ) and Add(string a, string b)
3-By passing sequence of arguments passed.
Add(int a , string b ) and Add(string a, int b)
Note-we cann't overload a function by return type of a function.

Virtual Functions
In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function which has the same signature as the base class function. This concept is an important concept of the polymorphism in object-oriented programming (OOP).
In object-oriented programming, when a derived class inherits from a base class, an object of the derived class may be referred to via a pointer or reference of the base class type instead of the derived class type. If there are base class methods overridden by the derived class, the method actually called by such a reference or pointer can be bound either 'early' (by the compiler), according to the declared type of the pointer or reference, or 'late' (i.e. by the runtime system of the language), according to the actual type of the object referred to.
Virtual functions are resolved 'late'. If the function is 'virtual' in the base class, the most-derived class's implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. If it is not 'virtual', the method is resolved 'early' and the function called is selected according to the declared type of the pointer or reference.
Virtual functions allow a program to call methods that don't necessarily even exist at the moment the code is compiled.

class Animal
{
void move(void)/*non-virtual*/ {

console.writeline("Animal Move in some direction");
}
virtual void eat(void) {}
};
// The class "Animal" may passes a definition for eat() if desired.
class Horse : public Animal
{
// The non virtual function move() is inherited but cannot be overridden
void eat(void)
{
console.writeline("Horse eat grass");
}
};
This allows a programmer to process a list of objects of class Animal, telling each in turn to eat by calling eat(), without needing to know what kind of animal may be in the list, how each animal eats, or what the complete set of possible animal types might be.


Abstract classes and pure virtual functions
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class.
Pure virtual methods typically have a declaration (signature) and no definition (implementation).
Classes containing pure virtual methods are called "abstract" classes and they cannot be instantiated directly. Derived class inherited from the abstract class has to implement all the pure virtual functions.

As an example, an abstract base class MathSymbol may provide a pure virtual function DoOperation(), and derived classes Plus and Minus implement doOperation() to provide concrete implementations. Implementing doOperation() would not make sense in the MathSymbol class, as MathSymbol is an abstract concept whose behaviour is defined solely for each given kind (subclass) of MathSymbol. Similarly, a given subclass of MathSymbol would not be complete without an implementation of DoOperation(). Now, if we call DoOperation() function, it runtime decides whether it needs to call DoOperation() for plus or DoOperation() for minus.