find and solve || findandsolve.com
Please wait.....

Polymorphism Example In C#

Polymorphism Example In C#

 

Polymorphism

Polymorphism
This is combination of tow word "poly" another "morphs".Poly means MULTIPLE and morphs meeas FORMS,so polymorphism means MULTIPLE FORMS OR many forms.This is  a Greek word that means many forms.This is one of the core principles of OOP after encapsulation and inheritance.

 

Polymorphism Overview

It gains all the methods when a derived class inherits from a base class.
The derived class can choose whether to
⦁    override virtual members in the base class.
⦁    inherit the closest base class method without overriding it.
⦁    in non-virtual implementation,that hide the base class implementations.

 

There are two types of Polymorphism in c# given below.
⦁    Static / Compile Time Polymorphism.
⦁    Dynamic / Runtime Polymorphism.

 

Static or Compile Time Polymorphism
The static or compile time polymorphism is achieved by method overloading and operator overloading in C#.It is also known as early binding.
Let's see a simple example of static or compile time polymorphism in C#.

 


using System;  
public class Staff {     public virtual void Salary()     {         Console.WriteLine("Get Salary...");     } }
public class Woman : Staff {     public override void Salary()     {         Console.WriteLine("Salary details...");     }
}
public class PolymorphismExample {     public static void Main()     {         Staff a = new Woman();         a.Salary();     } }

 

Output :
Salary details...
 

Dynamic or  Runtime Polymorphism
The dynamic  or runtime polymorphism is a achieved by method overriding.also known as late binding.

Let's see a another example of dynamic  or runtime polymorphism in C# where we are having two derived classes.

 


using System;
public class ExampleA {     public virtual void Display()     {         Console.WriteLine("Displaying A...");     } }
public class ExampleB : ExampleA {     public override void Display()     {         Console.WriteLine("Displaying B...");     }
}
public class ExampleC : ExampleA {     public override void Display()     {         Console.WriteLine("Displaying C...");     }
}
public class PolymorphismExample {     public static void Main()     {         ExampleA a;         a = new ExampleA();
        a.Display();
        a = new ExampleB();         a.Display();         a = new ExampleC();         a.Display();
    } }
Output:
Displaying  A...
Displaying  B...
Displaying  C...

 


Runtime Polymorphism with Data Members

 
using System;  
public class Vehicle {     public string color = "Red";
}
public class Car : Vehicle {
    public string color = "White"; }
public class SealedExample {     public static void Main()     {         Vehicle v = new Car();         Console.WriteLine(v.color);
    } }
OutPut:
Red

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response