C# -

Polymorphism

Polymorphism is a key concept in object-oriented programming that allows objects of different types to be treated as objects of a common super type. It enables a single interface to be used for a general class of actions, with specific behavior determined at runtime. This tutorial covers the basics of polymorphism, including method overriding, interfaces, and best practices.


1. Understanding Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. This is typically achieved through method overriding and interfaces.


2. Method Overriding

Method overriding is a form of polymorphism where a derived class provides a specific implementation of a method that is already defined in its base class. The virtual keyword is used in the base class, and the override keyword is used in the derived class.

Example:
        
            namespace InheritanceExamples;

// Base class
class Animal
{
    public string Name { get; set; }

    public virtual void MakeSound()
    {
        Console.WriteLine($"{Name} is making a sound.");
    }
}

// Derived class
class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine($"{Name} is barking.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog dog = new Dog { Name = "Buddy" };
        dog.MakeSound(); // Output: Buddy is barking.
    }
}
        
    

3. Interfaces and Polymorphism

Interfaces define a contract that implementing classes must adhere to. This allows different classes to be treated polymorphically through the interface.

Example:
        
            namespace PolymorphismExamples;

// Define an interface
interface IFlyable
{
    void Fly();
}

// Implement the interface in a class
class Bird : IFlyable
{
    public string Name { get; set; }

    public void Fly()
    {
        Console.WriteLine($"{Name} is flying.");
    }
}

// Another class implementing the same interface
class Airplane : IFlyable
{
    public string Model { get; set; }

    public void Fly()
    {
        Console.WriteLine($"The airplane model {Model} is flying.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        IFlyable bird = new Bird { Name = "Sparrow" };
        IFlyable airplane = new Airplane { Model = "Boeing 747" };

        bird.Fly();      // Output: Sparrow is flying.
        airplane.Fly();  // Output: The airplane model Boeing 747 is flying.
    }
}
        
    

4. Abstract Classes and Polymorphism

Abstract classes can also be used to achieve polymorphism. An abstract class can have abstract methods (without implementation) that derived classes must implement.

Example:
        
            namespace PolymorphismExamples;

// Abstract base class
abstract class Shape
{
    public abstract void Draw();
}

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

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

class Program
{
    static void Main(string[] args)
    {
        Shape circle = new Circle();
        Shape rectangle = new Rectangle();

        circle.Draw();      // Output: Drawing a circle.
        rectangle.Draw();   // Output: Drawing a rectangle.
    }
}
        
    

5. Best Practices for Polymorphism


Conclusion

Polymorphism is a powerful feature in C# that allows for more flexible and maintainable code. By understanding method overriding, interfaces, and abstract classes, you can effectively leverage polymorphism to create robust and scalable applications.