c-sharp
  1. c-sharp-polymorphism

C# Polymorphism

Polymorphism is a concept in object-oriented programming that allows objects of different types to be treated as if they're the same type. In C#, polymorphism is achieved through the use of inheritance, method overriding, and interfaces. In this tutorial, we'll discuss how to use polymorphism in C#.

Syntax

Polymorphism is achieved in C# through inheritance, method overriding, and interfaces. The syntax for creating a derived class that inherits from a base class is as follows:

class DerivedClass : BaseClass {
   // code
}

To override a method in the derived class, use the override keyword:

class DerivedClass : BaseClass {
   public override void MyMethod() {
      // code
   }
}

To implement an interface, use the following syntax:

class MyClass : MyInterface {
   // implementation of interface methods
}

Example

Let's say we have a base class called "Animal" and two derived classes, "Dog" and "Cat". The "Animal" class has a method called "MakeSound", which is overridden in the derived classes. Here's how we can implement it:

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

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

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

Now, we can create instances of the derived classes and call the "MakeSound" method:

Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound(); // Output: The dog barks.
myCat.MakeSound(); // Output: The cat meows.

Output

When we run the example code above, the output will be:

The dog barks.
The cat meows.

Explanation

In the example above, we defined a base class called "Animal" and two derived classes, "Dog" and "Cat". We then overridden the "MakeSound" method in the derived classes to produce animal-specific sounds.

We then created instances of the derived classes and assigned them to base class references. We called the "MakeSound" method on the base class references, which invoked the overridden method in the derived classes and produced the animal-specific sounds.

Use

Polymorphism is useful when you want to reuse code and avoid duplication. It allows you to treat objects of different types as if they're the same type, which can simplify your code and make it more readable.

Important Points

  • Polymorphism is achieved through inheritance, method overriding, and interfaces.
  • Use the virtual keyword to indicate that a method in the base class can be overridden.
  • Use the override keyword to override a method in the derived class.
  • Use interfaces to define a set of methods that multiple classes can implement.

Summary

In this tutorial, we discussed how to use polymorphism in C#. We covered the syntax, example, output, explanation, use, and important points of polymorphism in C#. With this knowledge, you can now use polymorphism in your C# code to reuse code, improve readability, and avoid duplication.

Published on: