A Comprehensive Resource for Microsoft Technologies

Welcome, your go-to destination for everything related to .NET and Microsoft technologies. With over 10 years of experience in the IT industry, I am excited to share my knowledge and insights on this platform. This blog is dedicated to providing valuable information, tips, and tutorials that are not only relevant to the rapidly evolving IT industry but also essential for individuals working on real-time projects and preparing for interviews

C# Tutorial 7.3: Polymorphism

 Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different types to be treated as objects of a common type. In C#, polymorphism is achieved through the use of interfaces, abstract classes, and virtual methods. Let's create a simple tutorial to understand polymorphism in C#.

1. Introduction to Polymorphism:

Polymorphism allows you to work with objects of different types through a common interface or base class. There are two types of polymorphism in C#:

  • Compile-time Polymorphism (Static Binding): Achieved through method overloading and operator overloading.
  • Run-time Polymorphism (Dynamic Binding): Achieved through method overriding.

2. Method Overloading:

Method overloading is a form of compile-time polymorphism where you can define multiple methods with the same name but with different parameters.

class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double a, double b)
    {
        return a + b;
    }
}

class Program
{
    static void Main()
    {
        Calculator calculator = new Calculator();
        Console.WriteLine(calculator.Add(5, 10));        // Calls the int version
        Console.WriteLine(calculator.Add(5.5, 10.5));   // Calls the double version
    }
}

3. Method Overriding:

Method overriding is a form of run-time polymorphism where a method in a base class is redefined in a derived class.

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

class Rectangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a rectangle");
    }
}

class Program
{
    static void Main()
    {
        Shape shape1 = new Circle();
        Shape shape2 = new Rectangle();

        shape1.Draw();  // Calls the Draw method of Circle
        shape2.Draw();  // Calls the Draw method of Rectangle
    }
}

4. Interface-based Polymorphism:

Interfaces provide a way to achieve polymorphism by defining a contract that classes must implement.

interface IShape
{
    void Draw();
}

class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

class Rectangle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a rectangle");
    }
}

class Program
{
    static void Main()
    {
        IShape shape1 = new Circle();
        IShape shape2 = new Rectangle();

        shape1.Draw();  // Calls the Draw method of Circle
        shape2.Draw();  // Calls the Draw method of Rectangle
    }
}

Conclusion:

Polymorphism is a powerful concept in C# that allows you to write more flexible and reusable code. Whether through method overloading, method overriding, or interfaces, polymorphism enables you to work with objects in a more abstract and generic way.