Fundamentals of Polymorphism in C#

Kailash Chandra Behera | Sunday, June 11, 2023

Introduction

Polymorphism facilitates that you can access objects of different types through the same interface. Here in this blog post, we will explore the Polymorphism of Object-Oriented-Programming(OOP).

Getting Started

Polymorphism is one of the core concepts of object-oriented programming. It means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation.

The word polymorphism is derived from two Greek words poly and morphos which means many and forms. Respectively the polymorphism in OOPs is often expressed by the phrase ‘one interface’, ‘multiple functions’ which means polymorphism allows one interface to be used for multiple functions. A human hand can be taken as one of the polymorphism examples, a human hand can be used to eat food, slap, hold an object etc.

Polymorphism Examples

A real-life example can be taken as polymorphism examples. An individual man can have different relationships with different people. He can be a Father, a Son, a brother and a fried. Polymorphism is the ability that helps in executing different operations in response to the same message, for example in OOPs you can implement it by creating more than one function in a class that have the same name.

Different Types of Polymorphisms

Based on the response time the polymorphism can be categorized into static polymorphism and dynamic polymorphism. Static polymorphism in C# responses to a function at compile time while dynamic polymorphism responses to a function at run time. This is the main difference between static and dynamic polymorphism.

Declaring multiple functions having same name with different parameters and comparison operator is static polymorphism example. Abstract class and Virtual function is the example of static polymorphism.

Static Polymorphism

Static polymorphism is a type of polymorphism that collects the information to call a method during compile time  static polymorphism can be implemented using Function Overloading and Operator Overloading.

Function Overloading

This approach allows you to declare multiple functions in a class having the same name but different in function arguments. Each redefinition of a function must use different types of parameters, sequence of parameters or number of parameters. The following code is an example of the function overloading.

 using System;  
 public class FunctionOverloadingDemo {  
   public string Display(int input) {  
    return Console.Write("The entered value is {0},input),  
   }  
  public string Display(string input) {  
    return Console.Write("The entered value is {0},input),  
   }  
   public static int Display(int input1, string input2) {  
     return Console.Write("The First entered value is {0} and Second entered value is{1},input1,input2),  
   }  
 }  

Operator Overloading

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. In C# Operator overloading is used to overload or redefines most of the operators available in C#. It is used to perform the operation on the user-defined data type. Operator overloading is a big concept in Object-Oriented-Programming; hence we will discuss it in another post.

Dynamic Polymorphism

Dynamic polymorphism is a type of polymorphism that collects information to call a method at run time. It is more useful as compared to static polymorphism because it gives much more flexibility for manipulating objects. Dynamic polymorphism is implemented using Abstract classes and Virtual functions.

Abstract Class C#

Abstract class is used to provide partial class implementation and the implementation will be completed after used in derived class. It allows to declare abstract methods which can be implemented by the derived class, about abstract method is given below. Note that an abstract class can have both abstract and regular methods.

There are certain rules governing the use of an abstract class. Those are:

  1. Cannot create an instance of an abstract class.
  2. Cannot declare an abstract method outside an abstract class.
  3. Cannot be declared sealed.
The keyword abstract is used to declare an abstract class. The following code is an example of the declaration of an abstract class.

 abstract class Animal   
 {  
  public abstract void Color();  
  public void sleep()   
  {  
   Console.WriteLine("Zzz");  
  }  
 }  

Abstract Method

Abstract method is a special type of method which does not have method body, meaning you can declare only the method inside the abstract class, but you cannot declare the body of it. The example of abstract method is given below.

 abstract class Animal   
 {  
  // abstract method  
  public abstract void Color();  
  // non-abstract method or concrete method  
  public void sleep()   
  {  
   Console.WriteLine("Zzz");  
  }  
 }  

In the above example color is a abstract method and sleep is not-abstract method or concrete method. A concreate method is a method that has method body and can directly call.

The implementation of an abstract method is done by the derived class. The derived class must override the abstract method when the derived class inherits the abstract method from the abstract class.

The syntax of declaring the abstract method is like below.

Virtual Method

A virtual method is a method that can be overridden in a derived class. When a method is declared as virtual in a base class, it allows a derived class to provide its own implementation of the method.To declare a method as virtual in C#, the "virtual" keyword is used in the method declaration in the base class.

The following code is an example of using virtual function.

 class Animal  
 {  
      public virtual void FoodHabits()  
      {  
           Console.Write("Animals have different food havits");  
      }  
 }  
 class Carnivorous:Animal  
 {  
      public override FoodHabits()  
      {  
           Console.Write("The Carnivorous animals eat only meat");  
      }  
 }  
 class Harbivorous:Animal  
 {  
      public override FoodHabits()  
      {  
           Console.Write("The Harbivorous animals eat only plants");  
      }  
 }  

Summary

Polymorphism allows a class to have multiple implementations with the same name. In this blog post Fundamentals of Polymorphism in C#, we explored the Polimorphis definition, the implementation, polymorphic examples and various types of Polimorphism. I hope this is helpful to you.

Thanks


No comments: