Sunday, 21 August 2016

Chapter -5 Polymorphism



1.      In Object-Oriented Programming (OOPs), polymorphism allows one interface to be used for multiple functions.
Effect of operator overloading may vary from one class to another class.
2.      When even an operator is overloaded it original functionality does not change an additional functionality is added.
3.      Polymorphism reduces the complexity within the functions of a class of a program.
4.      Polymorphism can either be static or dynamic.


Static Polymorphism
1.      Static polymorphism refers to an entity, which exists in various forms simultaneously.
2.      C# uses two approaches to implement static polymorphism.
These are:

     Function overloading: This approach allows using the same name for two or more functions. Functions should have different signature of parameter but return type should be same or different.

     Operator overloading: This approach allows user-defined types such as structures and classes, to use overloaded operators for easy manipulation of their objects. It is closely related to method overloading. To overload an operator you use operator keyword to create operator method.
      In dynamic polymorphism, the decision about function execution is made at run time.
      Dynamic polymorphism is more useful than static polymorphism as it provides much more flexibility for manipulating the objects.
     
 C# uses two approaches to implement dynamic polymorphism:
       Abstract classes: Are the special type of base classes that consist of abstract class members.
       Virtual functions: Are the functions that do not really exist, however, appear to be present in some parts of the program.

Function Overloading
      Function overloading is implemented by defining two or more functions in a class sharing the same name.
      In function overloading, each definition of a function must differ in its function signature.

Function Signature
      The signature of a function is defined by:
1.      The number of parameters
2.       The data types of parameters
3.      The sequence of the parameters

Demo of Function Overloading:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoFunctionOverloading
{
    class demo
    {public void display()
{
    Console.WriteLine("Display() without Parameter");
}
//Different Number of Paremters
public void display(int i)
{
    Console.WriteLine("Display() with different NO of Parameter");
}
//Different Sequence of Parameter
public void display(int i,string a)
{
    Console.WriteLine("Display() with different Sequence of Parameter i="+i+" and a="+a);
}
public void display(string a,int i)
{
    Console.WriteLine("Display() with different Sequence of Parameter a=" + a + " and i=" + i);
}
    }
    class Program
    {
static void Main(string[] args)
{
    demo d = new demo();
    d.display();
    d.display(1);
    d.display(2, "a");
    d.display("b", 3);
    Console.ReadLine();
}
    }
}

Output:-


Constructor Overloading
      Constructors can also be parameterized, and therefore, they can be overloaded.
      Overloaded constructors are commonly used in C# to provide flexibility while creating an object.
Click here for Constructor Overloading Demo 

Operator Overloading
      Operator overloading provides additional capabilities to C# operators when they are applied to user-defined data types.

      Only the predefined set of C# operators can be overloaded.

      To use operators with user-defined data types, they need to be overloaded according to a programmer’s requirement.

      The following table describes the overload ability of the operators in C#.

Operators
Description
+,  -, ! , ~, ++ , --
These unary operators take one operand and can be
overloaded.
+,  -, * , /, %
These binary operators take two operands and can be
overloaded.
==, !=, <, >, <=, >=
The comparison operators can be overloaded.
&&, ||
The conditional logical operators cannot be overloaded
directly, but they are evaluated using & and | which can be
overloaded.
+=, -=, *=, /=, %=
The assignment operators cannot be overloaded.
=, ., ?:, ->, new, is,
sizeof, typeof
These operators cannot be overloaded.

Demo for Operator Overloading:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OperatorOvlApplication
{
    class Box
    {
private double length;   // Length of a box
private double breadth;  // Breadth of a box
private double height;   // Height of a box

public double getVolume()
{
    return length * breadth * height;
}

public void setLength(double len)
{
    length = len;
}

public void setBreadth(double bre)
{
    breadth = bre;
}

public void setHeight(double hei)
{
    height = hei;
}

// Overload + operator to add two Box objects.
public static Box operator +(Box b, Box c)
{
    Box box = new Box();
    box.length = b.length + c.length;
    box.breadth = b.breadth + c.breadth;
    box.height = b.height + c.height;
    return box;
}

    }

    class Tester
    {
static void Main(string[] args)
{
    Box Box1 = new Box();   // Declare Box1 of type Box
    Box Box2 = new Box();   // Declare Box2 of type Box
    Box Box3 = new Box();   // Declare Box3 of type Box
    double volume = 0.0;    // Store the volume of a box here

    // box 1 specification
    Box1.setLength(6.0);
    Box1.setBreadth(7.0);
    Box1.setHeight(5.0);

    // box 2 specification
    Box2.setLength(12.0);
    Box2.setBreadth(13.0);
    Box2.setHeight(10.0);

    // volume of box 1
    volume = Box1.getVolume();
    Console.WriteLine("Volume of Box1 : {0}", volume);

    // volume of box 2
    volume = Box2.getVolume();
    Console.WriteLine("Volume of Box2 : {0}", volume);

    // Add two object as follows:
    Box3 = Box1 + Box2;

    // volume of box 3
    volume = Box3.getVolume();
    Console.WriteLine("Volume of Box3 : {0}", volume);
    Console.ReadKey();
}
    }
}

Output:-
 
Summary:-
1.      The term polymorphism has been derived form the Greek words ‘poly’ and ‘morphos’, which mean ‘many’ and ‘forms’, respectively.
2.      Polymorphism allows one interface to be used for multiple functions.
3.      Static polymorphism refers to an entity, which exists in different forms simultaneously.
4.      In dynamic polymorphism, the decision about function execution is made when code is executed.
5.      Function overloading is the process of using the same name for two or more functions in a class.
6.      The number, type, or sequence of parameters of a function is called its function signature.
7.      Overloaded constructors are commonly used in C# to provide flexibility while creating an object.
8.      Operator overloading provides additional capabilities to C# operators when they are applied to user-defined data types.
9.      The predefined C# operators can be overloaded by using the operator keyword.
10.  Operators may be considered as functions internal to the compiler.
I hope that the functionality of the above attribute is clear to you now. Now you can use this code in your application If you have further question, just drop a line below and I will try to answer you as soon as possible.
Last but not least, connect with me on Twitter , Facebook , LinkedIn and Google+ for technical updates and articles news. We won’t spam or share your email address as we respect your privacy.






No comments:

Post a Comment