C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Polymorphism

Polymorphism is one of the four fundamental principles of object-oriented programming (OOP) and allows objects of different classes to be treated as objects of a common superclass. In C#, polymorphism is implemented through inheritance and interfaces. This tutorial will cover the following topics related to polymorphism in C#:

  • Method overriding
  • Abstract classes and methods
  • Interfaces

Let's begin!

  • Method overriding

Method overriding is a form of runtime polymorphism that allows a subclass to provide a new implementation for a method that is already defined in its superclass.

Example:

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

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog barks.");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat meows.");
    }
}

In this example, the Animal class has a virtual method MakeSound. The Dog and Cat classes inherit from Animal and override the MakeSound method to provide their own implementations. Now, you can use these classes polymorphically:

Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Output: The dog barks.

myAnimal = new Cat();
myAnimal.MakeSound(); // Output: The cat meows.
  • Abstract classes and methods

Abstract classes are classes that cannot be instantiated and serve as a base class for other classes. Abstract methods are methods that have no implementation in the abstract class and must be overridden in any non-abstract derived class.

Example:

public abstract class Animal
{
    public abstract void MakeSound();
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The dog barks.");
    }
}

public class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("The cat meows.");
    }
}

In this example, the Animal class is an abstract class with an abstract method MakeSound. The Dog and Cat classes inherit from Animal and must provide their own implementations for the MakeSound method.

  • Interfaces

Interfaces define a contract that classes can implement. A class that implements an interface must provide an implementation for all the interface's members. Interfaces enable you to create polymorphic behavior without inheritance.

Example:

public interface IAnimal
{
    void MakeSound();
}

public class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("The dog barks.");
    }
}

public class Cat : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("The cat meows.");
    }
}

In this example, an IAnimal interface is defined with a MakeSound method. The Dog and Cat classes implement the IAnimal interface and provide their own implementations for the MakeSound method. Now, you can use these classes polymorphically:

IAnimal myAnimal = new Dog();
myAnimal.MakeSound(); // Output: The dog barks.

myAnimal = new Cat();
myAnimal.MakeSound(); // Output: The cat meows.

That's it! You've now learned the basics of polymorphism in C# using method overriding, abstract classes, and interfaces. Polymorphism allows you to create more flexible and reusable code by enabling objects of different classes to be treated as objects of a common superclass or interface.

  1. How to achieve polymorphism in C#

    Polymorphism allows objects of different types to be treated as objects of a common base type.

    // Base class
    class Shape
    {
        public virtual void Draw()
        {
            Console.WriteLine("Drawing a shape");
        }
    }
    
    // Derived class
    class Circle : Shape
    {
        public override void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    
    // Usage
    Shape shape = new Circle();
    shape.Draw(); // Calls the overridden method in Circle
    
  2. C# method overriding and polymorphism

    Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

  3. Polymorphic behavior in C# interfaces

    Interfaces enable polymorphic behavior by allowing multiple classes to implement the same set of methods.

    interface IDrawable
    {
        void Draw();
    }
    
    class Circle : IDrawable
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    
  4. Using polymorphism with abstract classes in C#

    Abstract classes can define abstract methods, providing a base for subclasses to implement.

    abstract class Shape
    {
        public abstract void Draw();
    }
    
    class Circle : Shape
    {
        public override void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    
  5. Dynamic polymorphism in C#

    Achieved through method overriding and runtime binding.

  6. Polymorphism and method hiding in C#

    Method hiding involves using the new keyword to hide a method in the base class.

    class BaseClass
    {
        public void Display()
        {
            Console.WriteLine("Base class display");
        }
    }
    
    class DerivedClass : BaseClass
    {
        new public void Display()
        {
            Console.WriteLine("Derived class display");
        }
    }
    
  7. Polymorphism and virtual methods in C#

    Marking a method as virtual in the base class allows derived classes to override it.

    class Shape
    {
        public virtual void Draw()
        {
            Console.WriteLine("Drawing a shape");
        }
    }
    
  8. C# polymorphism and interfaces

    Interfaces allow multiple classes to exhibit polymorphic behavior.