codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

20 / 60 topics
17Classes and Objects18Constructors in C#19Inheritance in C#20Polymorphism in C#21Encapsulation in C#22Access Modifiers in C#23Properties in C#24Indexers in C#25Events in C#26Delegates in C#
Tutorials/C# Programming/Polymorphism in C#
🔷C# Programming

Polymorphism in C#

Updated 2026-05-15
10 min read

Polymorphism in C#

Introduction

Polymorphism is a core concept in object-oriented programming (OOP) that allows objects to be treated as instances of their parent class. This enables a single interface to represent different underlying forms (data types). In C#, polymorphism can be achieved through method overriding and interfaces.

In this tutorial, we will explore how to implement polymorphism using method overriding and interfaces in C#. We'll cover the basics of these concepts and provide practical examples to illustrate their usage.

Concept

Method Overriding

Method overriding is a feature that allows a derived class to provide a specific implementation for a method that is already defined in its base class. This enables the derived class to modify or extend the behavior of the base class method.

To override a method, you must use the override keyword in the derived class and the virtual keyword in the base class.

Interfaces

An interface in C# is a contract that defines a set of methods, properties, events, or indexers that a class can implement. Interfaces provide a way to achieve polymorphism by allowing objects of different classes to be treated as instances of the same interface.

To implement an interface, a class must provide implementations for all the members defined in the interface.

Examples

Method Overriding Example

Let's start with an example of method overriding. We'll create a base class Animal and a derived class Dog. The Dog class will override the MakeSound method from the Animal class.

// Base class
public class Animal
{
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

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

In this example, the `Animal` class has a virtual method `MakeSound`. The `Dog` class overrides this method to provide its own implementation.

Now, let's create an instance of `Dog` and call the `MakeSound` method:

```csharp
public class Program
{
    public static void Main()
    {
        Animal myAnimal = new Dog();
        myAnimal.MakeSound(); // Output: Woof!
    }
}

Here, we've created a variable of type `Animal` but assigned it an instance of `Dog`. When we call the `MakeSound` method on this variable, the overridden method in the `Dog` class is executed.

### Interface Example

Next, let's look at how interfaces can be used to achieve polymorphism. We'll create an interface `IVehicle` and two classes `Car` and `Bike` that implement this interface.

```csharp
// Interface
public interface IVehicle
{
    void Start();
}

// Class implementing the interface
public class Car : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Car is starting");
    }
}

// Another class implementing the interface
public class Bike : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Bike is starting");
    }
}

In this example, both Car and Bike classes implement the IVehicle interface by providing their own implementation of the Start method.

Now, let's create instances of these classes and call the Start method:

public class Program
{
    public static void Main()
    {
        IVehicle myCar = new Car();
        IVehicle myBike = new Bike();

        myCar.Start(); // Output: Car is starting
        myBike.Start(); // Output: Bike is starting
    }
}

Here, we've created variables of type IVehicle but assigned them instances of Car and Bike. When we call the Start method on these variables, the appropriate implementation in each class is executed.

What's Next?

In the next section, we will explore encapsulation in C#. Encapsulation is another fundamental concept in OOP that helps to protect the internal state of an object and provide controlled access to it. Stay tuned!


PreviousInheritance in C#Next Encapsulation in C#

Recommended Gear

Inheritance in C#Encapsulation in C#