5. Explain the concept of polymorphism in C# with an example.

Basic

5. Explain the concept of polymorphism in C# with an example.

Overview

Polymorphism is a fundamental concept in C# and object-oriented programming that allows methods to do different things based on the object that it is acting upon, even though they share the same name. This enables objects of different classes to be treated as objects of a common super class, providing flexibility and reusability in code components.

Key Concepts

  1. Static Polymorphism (Compile-time): Achieved through method overloading.
  2. Dynamic Polymorphism (Run-time): Implemented via method overriding using virtual and override keywords.
  3. Interface Polymorphism: Using interface methods to implement polymorphic behavior.

Common Interview Questions

Basic Level

  1. What is polymorphism and why is it important in C#?
  2. Can you provide a simple example of method overloading in C#?

Intermediate Level

  1. Explain how method overriding works in C#.

Advanced Level

  1. Discuss how polymorphism can be achieved through interfaces in C# and its benefits over inheritance.

Detailed Answers

1. What is polymorphism and why is it important in C#?

Answer: Polymorphism is a core concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It's important in C# for several reasons:
- Reusability: Enables the reuse of code and makes the system more modular.
- Scalability: Facilitates adding new features with minimal changes to the existing code.
- Maintainability: Enhances the maintainability of the code by allowing changes to be made at a higher level.

Key Points:
- Allows methods to perform different tasks based on the object that invokes them.
- Improves code readability and reusability.
- Supports the open/closed principle, allowing systems to be open for extension but closed for modification.

Example:

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("Some sound");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Bark");
    }
}

public class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Meow");
    }
}

2. Can you provide a simple example of method overloading in C#?

Answer: Method overloading is a type of static polymorphism where multiple methods in a class have the same name but differ in the type or number of parameters. It allows a class to have several methods with the same name but with different signatures.

Key Points:
- Compile-time polymorphism.
- Improves code readability and usability.
- The return type can be different as long as the parameter signature is different.

Example:

public class MathOperations
{
    // Method to add two integers
    public int Add(int a, int b)
    {
        return a + b;
    }

    // Overloaded method to add three integers
    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
}

3. Explain how method overriding works in C#.

Answer: Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is achieved using the virtual keyword in the base class method and the override keyword in the derived class method.

Key Points:
- Enables runtime polymorphism.
- The overriding method must have the same name, return type, and parameters as the method in the base class.
- Base class method must be marked with the virtual, abstract, or override keyword.

Example:

public abstract class Shape
{
    public virtual double Area()
    {
        return 0;
    }
}

public class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double Area()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

4. Discuss how polymorphism can be achieved through interfaces in C# and its benefits over inheritance.

Answer: Polymorphism can also be achieved through interfaces in C#. An interface defines a contract (a set of methods and properties) that implementing classes must fulfill. Interface polymorphism allows different classes to implement the same interface in different ways, thus achieving polymorphism.

Key Points:
- Provides a way to achieve polymorphism where multiple inheritance is not supported.
- Encourages loose coupling, making systems more flexible and easier to modify.
- Interfaces can define properties, methods, events, and indexers that the implementing classes must provide an implementation for.

Example:

public interface IShape
{
    double Area();
}

public class Square : IShape
{
    public double Side { get; set; }

    public Square(double side)
    {
        Side = side;
    }

    public double Area()
    {
        return Side * Side;
    }
}

public class Circle : IShape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double Area()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

This example demonstrates how different shapes (Square and Circle) implement the IShape interface, each providing its own implementation of the Area method. This is a clear example of polymorphism through interfaces, allowing objects of different classes to be treated through a common interface reference.