Inheritance vs Polymorphism: Key Differences

INHERITANCE VS POLYMORPHISM IN C#

by

Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP), especially in C#. These concepts play a critical role in designing and building efficient, reusable, and well-structured code. If you're preparing for a .NET job interview, this is the perfect place to brush up on inheritance and polymorphism. In this tutorial, you will learn the differences between inheritance and polymorphism, their benefits, and some practical C# code examples.

Inheritance in C#

Inheritance is a mechanism that allows one class to inherit the properties and methods of another class. In other words, inheritance allows you to create a new class based on an existing one, which helps you reuse code and reduce complexity. The existing class is called the "base class" or "parent class," while the new class is called the "derived class" or "child class."

In C#, you can create a derived class using the : symbol followed by the base class name. Let's look at an example to understand how inheritance works:

public class Animal
{
    public void Eat()
    {
        Console.WriteLine("The animal is eating.");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("The dog is barking.");
    }
}

In this example, we have a base class Animal with a method Eat(). We then create a derived class Dog that inherits from the Animal class. The Dog class also has its own method Bark(). Now, any instance of the Dog class can access both the Eat() and Bark() methods.

Polymorphism in C#

Polymorphism is a concept that allows objects of different classes to be treated as objects of a common superclass. It enables you to write more flexible and extensible code by allowing a single interface to represent different types of objects. Polymorphism is achieved in C# through method overriding and interfaces.

Method Overriding

Method overriding is a technique that allows a derived class to provide a new implementation for a method that already exists in its base class. The method in the derived class must have the same name, return type, and parameters as the method in the base class. To override a method, you need to use the override keyword in the derived class and the virtual keyword in the base class.

Here's an example of method overriding in C#:

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 the Animal class and override the MakeSound() method with their own implementations. Now, when you call the MakeSound() method on a Dog or Cat object, the respective overridden method will be executed.

Interfaces

Interfaces are another way to achieve polymorphism in C#. An interface is a contract that defines a set of methods and properties that a class must implement. A class that implements an interface must provide an implementation for all of its methods and properties. You can use the interface keyword to define an interface and the : symbol to implement an interface in a class.

Here's an example of using an interface in C# for polymorphism:

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, we have an IAnimal interface 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 treat Dog and Cat objects as IAnimal objects and call the MakeSound() method on them.

The Bottom Line

Inheritance and polymorphism are two essential concepts in C# and OOP that play a critical role in designing and building efficient, reusable, and well-structured code. Inheritance allows you to create a new class based on an existing one, while polymorphism enables you to treat objects of different classes as objects of a common superclass.


Don't stop learning!

There is so much to discover about C#. That's why I am making my favorite tips and tricks available for free. Enter your email address below to become a better .NET developer.


Did you know?

Our beautiful, multi-column C# reference guides contain more than 150 tips and examples to make it even easier to write better code.

Get your cheat sheets