OOP .NET Interview question :- What is Polymorphism?

 Posted by Questpond on 2/9/2011 | Category: .NET Framework Interview questions | Views: 11085 | Points: 40
Answer:

This is a nice and simple OOP interview question.

As the name says POLY (Many) MORPHISM (Change). Polymorphism is the ability of the object to behave differently under different conditions. For instance a simple user object can behave as a admin user or as a simple user depending on conditions.

There are two ways of doing polymorphism dynamic and static. Static polymorphism is achieved by using method overloading and dynamic polymorphism is achieved by using method overriding. Please note do confuse with overloading and overriding while answering this question. Many .NET developers make a nice versa statement leading to lot of confusion.

.NET interview question and answer http://www.questpond.com


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Muhsinathk on: 6/11/2012 | Points: 10
Poly means many. So how can we take this definition in .NET context. Well we can apply this to class’s ability to share the same methods (actions) but implement them differently.

Suppose we have a method in a class to add numbers,



public class calculation

{

public int add(int x, int y)

{

return x+y;

}

public int add(int x, int y,int z)

{

return x+y+z;

}

}

Types of Polymorphism

1) Static or Compile time Polymorphism

Which method is to be called is decided at compile-time only. Method Overloading is an example of this. Method overloading is a concept where we use the same method name many times in the same class, but different parameters. Depending on the parameters we pass, it is decided at compile-time only. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permit. In Static Polymorphism decision is taken at compile time.

public Class StaticDemo

{

public void display(int x)

{

Console.WriteLine(“Area of a Square:”+x*x);

}

public void display(int x, int y)

{

Console.WriteLine(“Area of a Square:”+x*y);

}

public static void main(String args[])

{

StaticDemo spd=new StaticDemo();

Spd.display(5);

Spd.display(10,3);

}

}
2) Dynamic or Runtime Polymorphism.

Run time Polymorphism also known as method overriding. In this Mechanism by which a call to an overridden function is resolved at a Run-Time (not at Compile-time) if a base Class contains a method that is overridden. Method overriding means having two or more methods with the same name, same signature but with different implementation. In this process, an overridden method is called through the reference variable of a superclass, the determination of the method to be called is based on the object being referred to by reference variable.

Class BaseClass

{

Public void show ()

{

Console.WriteLine(“From base class show method”);

}

}

Public Class DynamicDemo : BaseClass

{

Public void show()

{

Console.WriteLine(“From Derived Class show method”);

}

Public static void main(String args[])

{

DynamicDemo dpd=new DynamicDemo ();

Dpd.show();


}

}

Login to post response