Polymorphism In C#

C# is a genuine object-oriented programming language. It Implements all of the major OOP features including polymorphism. Polymorphism and virtual functions go hand in hand. In fact polymorphism is achieved using virtual methods. Polymorphism allows you to implement methods of the derived class during run-time. Polymorphism sounds confusing at the beginning thus, many times people tend to skip this topic. It takes a little patience and dedication to learn the advantages offered by this OOP feature.

Looking at some code should help make things clear.

Consider Example 1,

Example 1:

Polymorphism in C# Example 1

In Example 1 we create a class DrawObj, in which we define a virtual method called Draw(). All that the Draw() method does is that it displays a message on the user’s screen.

A point to note here is that the Draw() method is a virtual method. Note the syntax, it is similar to the one for a normal method except that the virtual keyword needs to be specified.

Virtual functions come in handy when we need to call the derived class method from an object of the base class.

Suppose you need to assign a group of objects to an array and then invoke each of their methods. They will not necessarily have to be the same object type. However, if they are 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. This may sound a bit confusing. The following example will make things clearer.

Consider Example 2 as an extension of Example 1.

Example 2:

Polymorphism in C# Example 2

In Example 2, we have defined three classes (namely Line, Circle and Square). All of them are derived from the class DrawObj and all of them override the Draw() method of the DrawObj class.

So far we have been building the program part by part. Next we shall write the Main() function of the program, which will bring all these pieces (and classes) of code together, in Example 3.

Example 3:

Polymorphism in C# Example 3

If we merge the code in Examples 1, 2 and 3, compile and run, the output will be,

This is the Virtual Draw method
This is the Draw() method of Line
This is the Draw() method of Circle
This is the Draw() method of Square

Note the output. The Draw() method for each and every class derived from DrawObj has been called. However, we have written the Draw() method only once. Then how did this happen? Simple, note that the Draw() method has been written inside the foreach loop, which iterates through the DrawObj type objects in the array ObjD and calls the Draw() method. You would want to know how the method of the derived class is called while the array is of type DrawObj (the base class). This happened just because the code that we wrote exhibits polymorphism.

In the Main() function of Example 3, we have declared an array called ObjD to hold four objects of the type DrawObj. Next we have initialized the array with objects of the derived classes (namely line, circle and square). The first element being, of course, the DrawObj itself. We are allowed to assign the objects to an array of the base class because of the inheritance relationship that the derived class objects have with the DrawObj. Inheritance allows derived objects to act like their base class objects, which saves us a lot of coding. If this was not possible we would end up creating a different array for each type.

In case you need another example to clear doubts you might have on polymorphism, consider Example 4.

Example 4:

Polymorphism in C# Example 4

The output of Example 4 will be:

600
    • Note that the MethodA() is not overridden in class B. When we call MethodA() from an object of class B, the MethodA() of class A gets called as class B is inherited from class A.
    • Now MethodA() of class A needs to call MethodB(). There exists a MethodB() in class A as well as in class B.
    • Now if we were to use the normal method overriding, and had not defined our overridden methods as virtual, the MethodB() of class A would have been called and the output would have been 200.
    • Next, we have specified the MethodB() of class A as virtual and also overridden this MethodB() of class A in class B.
    • Since the call to MethodA() has come in from class B, the overridden method in class B is called.
    • Polymorphism is not just overriding it is intelligent overriding.
    • The difference between overriding and polymorphism is that the decision as to which method to call (whether the base class one or the class from which the object was instantiated) is made at runtime!
    • Remember, Polymorphism requires virtual functions, and virtual functions in turn require method overriding, that is the association between polymorphism and overriding.

Leave a comment