C# and .NET interview questions – Major difference between static polymorphism and dynamic polymorphism?

Static Polymorphism: –  Static Polymorphism means “Compile” time polymorphism or “Early binding”.

Method Overloading is an example of Static Polymorphism, where method name is same with different parameters and implementation.

Example: –

Below is the simple code snippet for Method Overloading.

public class StaticPoly
{
public void Display(int x)
{
Console.WriteLine("Value of x:"+x);
}
public void Display(int x, int y)
{
int z = 0;
z = x + y;
Console.WriteLine("Addition of x and y is:"+ z);
}
static void Main(string[] args)
{
StaticPoly Obj = new StaticPoly();
Obj.Display(10);
Obj.Display(10,3);
Console.ReadLine();
}
}

In the above code snippet you can see that we have two methods with same name but with different parameters and implementation.

Dynamic polymorphism
: – Dynamic polymorphism means “Runtime” time polymorphism or “Late binding”.

Method Overriding is an example of Dynamic Polymorphism, where the base class method is overridden by override keyword and its implementation is also changed.

Example: –

Below is the simple code snippet for Method Overriding.

class A
{
public virtual void Display()
{
System.Console.WriteLine("Base Class A::Display");
}
}

class B : A
{
public override void Display()
{
System.Console.WriteLine("Derived Class B::Display");
}
}

class Demo
{
public static void Main()
{
A b;
b = new A();
b.Display();

b = new B();
b.Display();
Console.ReadLine();
}
}

In the above code snippet you can see that we have two methods with same name,same signature but with different implementations.

View following one of most asked question on “What is Generics?”

See more videos on C# interview questions and answers

Regards,

View authors more tutorials on Most asked c# interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s