UPCOMING ACTIVITIES

greenOpen Discussion - March 11

more details

debateDebate Night - March 16

more details

karaokeKaraoke Night - March 12

more details

balbianelloWaiting for Balbianello Trip - March 23

more details

Home arrow News arrow emag: Polymorphism
emag: Polymorphism PDF Print E-mail
Wednesday, 02 December 2009

esrfgsgby Usman Muhammad

Polymorphism: is one of the core concept in Object Oriented Programming. we are highly thankful to Mr. usman Muhammad, who makes a really fantastic effort to start this topic as Learning Series. So, don't miss the chance to polish your abilities with this great opportunity and Be prepared for the upcoming series on this topic...

Click here to read the article.

What is Polymorphism?

Polymorphism and encapsulation are two big words in OO development, and are also a fundamental concept of software development.

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly refers to ‘many’, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism in a generic term means 'many shapes'.

There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions.

Overloading occurs when a method has more than one definition in the same scope. It's important to remember two key points from the previous statement: same name and same scope. The method implementations have the same name because they do similar tasks. For instance, if we need to implement a method that gets the student name, there are many ways to do that. We can get the name using an id and we can get the name using a social security number.

The first question that comes to mind is: do we have to declare them with the same name? The answer is no. So overloading is not mandatory; it is just a helpful feature in object-oriented languages. It is common sense since both methods are doing pretty much the same thing.

Because the methods have the same name, the compiler will use the signature to determine what method to call under different scenarios. The compiler will be able to tell the difference using the method signature, which has to do with the method parameters. Be definition, the method signature is the name of the method and its parameters. Since the name in this case is the same, what is left is the parameters. There are three ways to distinguish one set of parameters from the other:

·         Parameter count - one method could have 2 parameters while the other has 3. This way, the compiler will call the method with the correct number of parameters.

·         Parameter type - if two methods have the same parameter count, but different types (int, string, long …), then it is easy for the compiler to know which method to call.

·         Parameter order - when we have a match in the parameter count and parameter type, but with a different order, then it is again easy for the compiler to know which method to call.

 

public class AddingNumbers

{

    public int Add(int a, int b)

    {

        return a+b;

    }

    public int Add(int a, int b, int c)

    {

        return a+b+c;

     }  

}

 Similarly in polymorphism operators can also be overloaded. Operators can be overloaded in order to perform special functions with respect to the class. With the help of operator overloading standard operations such as + , - , * , etc can be applied on the objects of the class.

Overriding is the process of defining a function in the child class with same name. In this way the child class method hides parent class method. Method overriding is used to provide different implementations of a function so that a more specific behavior can be realized.

It allows you to invoke derived class methods through a base class reference during run-time. This is handy when you 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. However, if they're related by inheritance, you can add them to the array as the inherited type. Then if they all share the same method name, that method of each object can be invoked.

usmanmuh

 

using System;

public class DrawingObject

{

    public virtual void Draw()

    {

        Console.WriteLine("I'm just a generic drawing object.");

    }

}

 

using System;


public class Line : DrawingObject

{
    public override void Draw()

    {

        Console.WriteLine("I'm a Line.");

    }

}


public class Circle : DrawingObject

{

    public override void Draw()

    {

        Console.WriteLine("I'm a Circle.");

    }

}


public class Square : DrawingObject

{

    public override void Draw()

    {

        Console.WriteLine("I'm a Square.");

    }

}

 

using System;

 

public class DrawDemo

{

    public static int Main( )

    {

        DrawingObject[] dObj = new DrawingObject[4];


        dObj[0] = new Line();

        dObj[1] = new Circle();

        dObj[2] = new Square();

        dObj[3] = new DrawingObject();


        foreach (DrawingObject drawObj in dObj)

        {

            drawObj.Draw();

        }


        return 0;

    }

}

Output:

        I'm a Line.

        I'm a Circle.

        I'm a Square.

        I'm just a generic drawing object.

For any Further Reference or more information about this Article, you may contact with Usman Muhammad at \n \n This email address is being protected from spam bots, you need Javascript enabled to view it This email address is being protected from spam bots, you need Javascript enabled to view it This email address is being protected from spam bots, you need Javascript enabled to view it

More information and leaning will be continued in the upcoming series!!! …..:-)



 
< Prev   Next >