Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Static Polymorphism
3.
Function Overloading
3.1.
CODE
4.
Dynamic Polymorphism
5.
Important points of Abstract Class
5.1.
Code
5.2.
Code
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

C# Polymorphism

Author Nishant Rana
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Polymorphism refers to “having many forms”. This concept of Object-Oriented Paradigm explains the situation where something occurs in many different forms. 

Polymorphism can be of two types:

  1. Static Polymorphism: In this polymorphism, the information for calling the function is collected at the compile time.
  2. Dynamic Polymorphism: In this polymorphism, the call to an overridden method is resolved at the run time rather than compile time.

Recommended Topic, Palindrome in C#, singleton design pattern in c#, and Ienumerable vs Iqueryable.

Static Polymorphism

It is a process of linking the method/function with the object at the Compile time. It is also known as early binding or status binding. In this polymorphism, the information for calling the function is collected at the compile time.

In C#, we can achieve Static Polymorphism using function overloading.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Function Overloading

We can have many functions with the same name and different implementations in the same scope. It is just that the definition of the functions must differ from each other based on the type of arguments or the number of arguments passed. We can not overload a function based on the return type of the function.

Let us see an example where we will overload the print() function that will print different data type variables.

CODE

using System;


namespace PolymorphismExample {
   class Printdata {
      void print(int i) {
         Console.WriteLine("Printing int: {0}", i );
      }
      void print(double f) {
         Console.WriteLine("Printing float: {0}" , f);
      }
      void print(string s) {
         Console.WriteLine("Printing string: {0}", s);
      }
      static void Main(string[] args) {
         Printdata p = new Printdata();
         
         // Call print to print integer
         p.print(5);
         
         // Call print to print float
         p.print(500.263);
         
         // Call print to print string
         p.print("Hello Java");
         Console.ReadKey();
      }
   }
}

Output

Printing int: 5
Printing float: 500.263
Printing string: Hello Java

Dynamic Polymorphism

In C#, we can create abstract classes that provide a partial implementation of the class. We can just define the methods in the abstract class without its implementation. These methods are known as abstract methods. Whenever a class derives the abstract class, it needs to implement the abstract methods.

Important points of Abstract Class

  1. We can create an object/instance of an abstract class.
  2. We can only declare the abstract method inside the abstract class.
  3. We can declare the abstract classes to be sealed.

Code

using System;


namespace PolymorphismApplication {
   abstract class Shape {
      public abstract int area();
   }
   
   class Rectangle:  Shape {
      private int length;
      private int width;
      
      public Rectangle( int a = 0, int b = 0) {
         length = a;
         width = b;
      }
      public override int area () { 
         Console.WriteLine("Rectangle class area :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}

Output

Rectangle class area :
Area: 70

We can also achieve Dynamic Polymorphism by inheritance. If we want some functions defined in the base class to be implemented again in the derived classes, then we can use virtual functions.

 

For example:-

Let us say, we have a vehicle class. It has a method//function named numberOfTires(It returns the number of tires of the vehicle). 

The derived classes of the vehicle class can be a car, bus, bike, etc. Each of these derived classes has a different number of tires. Hence, they would implement their own numberOfTires function in their way.

Code

using System;


class Vehicle  // Base class (parent) 
{
  public virtual void numberOfTires() 
  {
    Console.WriteLine("The vehicle has 8 tires");
  }
}


class car : Vehicle  // Derived class (child) 
{
  public override void numberOfTires() 
  {
    Console.WriteLine("The car has 4 tires");
  }
}


class bus : Vehicle  // Derived class (child) 
{
  public override void numberOfTires() 
  {
    Console.WriteLine("The bus has 8 tires");
  }
}


class bike : Vehicle  // Derived class (child) 
{
  public override void numberOfTires() 
  {
    Console.WriteLine("The bike has 2 tires");
  }
}


class Program 
{
  static void Main(string[] args) 
  {
    Vehicle myVehicle = new Vehicle();  // Create a Animal object
    Vehicle myBike = new bike();  // Create a Pig object
    Vehicle myBus = new bus();  // Create a Dog object


    myVehicle.numberOfTires();
    myBike.numberOfTires();
    myBus.numberOfTires();
  }
}

Output:

The vehicle has 8 tires
The bike has 2 tires
The bus has 8 tires

FAQs

  1. What is Polymorphism?
    Polymorphism refers to “having many forms”. This concept of Object-Oriented Paradigm explains the situation where something occurs in many different forms.
  2. Why are different types of Polymorphism?
    Polymorphism can be of two types:
    1. Static Polymorphism: In this polymorphism, the information for calling the function is collected at the compile time.
    2. Dynamic Polymorphism: In this polymorphism, the call to an overridden method is resolved at the run time rather than compile time.

Key Takeaways

In this article, we have extensively discussed the following things:

  1. We first discussed what is Polymorphism.
  2. Then we discussed the different types of Polymorphism.
     

We hope that this blog has helped you enhance your knowledge regarding Polymorphism in the GO language and if you would like to learn more, check out our articles here

Check out this article - Compile Time Polymorphism

Recommended Readings:

Do upvote our blog to help other ninjas grow. Happy Coding!



 

Previous article
Inheritance in C#
Next article
Multiple Inheritance in C#
Live masterclass