Home | | C Sharp and .NET Framework(CNF) | C# Polymorphism

Chapter: C# and .NET Framework

C# Polymorphism

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means ―multiple‖ and morph means ―forms‖ so polymorphism means many forms.

POLYMORPHISM

 

Polymorphism means many forms (ability to take more than one form). In Polymorphism poly means ―multiple‖ and morph means ―forms‖ so polymorphism means many forms.

 

Ø   In polymorphism we will declare methods with same name and different parameters in same class or methods with same name and same parameters in different classes. Polymorphism has ability to provide different implementation of methods that are implemented with same name.

 

Ø   In Polymorphism we have 2 different types those are

 

-      Compile Time Polymorphism (Called as Early Binding or Overloading or static binding)

-      Run Time Polymorphism (Called as Late Binding or Overriding or dynamic binding)

 

Compile Time Polymorphism

 

Ø   Compile time polymorphism means we will declare methods with same name but different signatures because of this we will perform different tasks with same method name. This compile time polymorphism also called as early binding or method overloading.

 

Ø   Method Overloading or compile time polymorphism means same method names with different signatures (different parameters)

 

Example

public class Class1

 

{

public void NumbersAdd(int a, int b)

{

Console.WriteLine(a + b);

}

public void NumbersAdd(int a, int b, int c)

{

Console.WriteLine(a + b + c);

}

}

 

Ø   In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding.

 

Run Time Polymorphism

 

Ø   Run time polymorphism also called as late binding or method overriding or dynamic polymorphism. Run time polymorphism or method overriding means same method names with same signatures.

 

Ø   In this run time polymorphism or method overriding we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance principle and using ―virtual & override‖ keywords.

 

Ø   In base class if we declare methods with virtual keyword then only we can override those methods in derived class using override keyword

 

Example

 

//Base Class public class Bclass

{

 

public virtual void Sample1()

{

Console.WriteLine("Base Class");

}

}

// Derived Class

public class DClass : Bclass

{

public override void Sample1()

{

 

Console.WriteLine("Derived Class");

}

}

 

//  Using base and derived class class Program

 

{

static void Main(string[] args)

{

 

//  calling the overriden method DClass objDc = new DClass(); objDc.Sample1();

 

//  calling the base class method Bclass objBc = new DClass(); objBc.Sample1();

 

}

}

If we run above code we will get output like as shown below

 

Output

----------------------------------

 

Derived Class

Derived Class

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
C# and .NET Framework : C# Polymorphism |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.