Pages

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.

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.