Polymorphism - potatoscript/csharp GitHub Wiki

🥔 Polymorphism in C# 🥔

🥔 What is Polymorphism? 🥔

Polymorphism is another core concept of object-oriented programming (OOP). The word polymorphism comes from Greek, meaning "many forms." In programming, it refers to the ability of different classes to respond to the same method or message in different ways.

In simpler terms, polymorphism allows us to use a single method or function name to perform different tasks based on the object that is calling it.

Think of it like this: A dog and a cat both have a method called MakeSound(), but each will make a different sound when the method is called. The dog might say "Woof!", and the cat might say "Meow!" Even though they share the same method name, they behave differently.

🥔 Why Do We Use Polymorphism? 🥔

  • Flexibility: It allows the same method to be used on different types of objects, making the code flexible and easier to extend.
  • Code Reusability: By using polymorphism, we don't need to write separate methods for every class; one method can work for multiple classes.
  • Simplifies Code: It simplifies code by allowing you to interact with different objects through a common interface, making the code cleaner and easier to understand.

🥔 How Does Polymorphism Work in C#? 🥔

Polymorphism works mainly in two ways:

  1. Method Overloading (Compile-Time Polymorphism)
  2. Method Overriding (Run-Time Polymorphism)

Let's dive into both types of polymorphism with examples.


🥔 1. Method Overloading (Compile-Time Polymorphism) 🥔

Method overloading occurs when you create multiple methods with the same name but with different parameters (number or type).

In other words, the method name is the same, but the way it is called changes based on the arguments passed to it.

🥔 Example of Method Overloading 🥔

Let's say we want to create a method to add numbers, but it should be able to handle both two integers and three integers.

public class Calculator
{
    // Method to add two integers
    public int Add(int a, int b)
    {
        return a + b;
    }

    // Method to add three integers
    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
}

🥔 Explanation 🥔

  • The Add method is overloaded because it has the same name but accepts different numbers of parameters (two or three).
  • The compiler decides which method to call based on the number of arguments passed when the method is called.

🥔 Using the Overloaded Methods 🥔

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();

        // Call the method with two parameters
        Console.WriteLine(calc.Add(2, 3)); // Output: 5

        // Call the method with three parameters
        Console.WriteLine(calc.Add(2, 3, 4)); // Output: 9
    }
}

🥔 Explanation 🥔

  • Both calls to Add use the same method name, but the compiler figures out which method to call based on the number of parameters provided.

🥔 2. Method Overriding (Run-Time Polymorphism) 🥔

Method overriding occurs when a method in a child class has the same name and signature as a method in the parent class, but the child class provides its own implementation.

When you call a method on an object, C# will decide at run time which version of the method to call, depending on the type of object that is calling it. This is known as run-time polymorphism.

🥔 Example of Method Overriding 🥔

Let's extend our Animal example to demonstrate method overriding. The parent class will have a method MakeSound(), and each child class (like Dog and Cat) will provide their own implementation of this method.

// Parent class
public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("This animal makes a sound.");
    }
}

// Child class: Dog
public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof! Woof!");
    }
}

// Child class: Cat
public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow! Meow!");
    }
}

🥔 Explanation 🥔

  • The MakeSound() method is marked as virtual in the parent class (Animal), indicating that it can be overridden in the child classes.
  • The Dog and Cat classes override the MakeSound() method to provide their own specific implementation.

🥔 Using Overridden Methods 🥔

class Program
{
    static void Main()
    {
        // Create objects of Dog and Cat
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        // Call the overridden method
        myDog.MakeSound(); // Output: Woof! Woof!
        myCat.MakeSound(); // Output: Meow! Meow!
    }
}

🥔 Explanation 🥔

  • The method MakeSound() is called on both the myDog and myCat objects.
  • Even though both objects are declared as type Animal, the actual type of the object (either Dog or Cat) determines which version of MakeSound() is called at run time.