C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 8

What is polymorphism in C#?

Answer:

Polymorphism is one of the core concepts of object-oriented programming (OOP) in C#. It allows objects of different types to be treated as objects of a common base type. Polymorphism provides a way to perform a single action in different forms. In C#, polymorphism is mainly achieved through method overriding and method overloading.

Types of Polymorphism

  1. Compile-time Polymorphism (Static Polymorphism):

    • Achieved through method overloading and operator overloading.
    • The decision about which method to invoke is made at compile time.
  2. Runtime Polymorphism (Dynamic Polymorphism):

    • Achieved through method overriding.
    • The decision about which method to invoke is made at runtime based on the object type.

Method Overloading (Compile-time Polymorphism)

Method overloading allows multiple methods in the same class to have the same name but different parameters (different type or number of parameters). The correct method is chosen based on the method signature at compile time.

Example:

public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public double Add(double a, double b)
    {
        return a + b;
    }

    public int Add(int a, int b, int c)
    {
        return a + b + c;
    }
}

class Program
{
    static void Main()
    {
        Calculator calc = new Calculator();
        Console.WriteLine(calc.Add(5, 10));        // Calls Add(int, int)
        Console.WriteLine(calc.Add(5.5, 10.5));    // Calls Add(double, double)
        Console.WriteLine(calc.Add(1, 2, 3));      // Calls Add(int, int, int)
    }
}

Method Overriding (Runtime Polymorphism)

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. The virtual keyword is used in the base class, and the override keyword is used in the derived class.

Example:

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

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

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

class Program
{
    static void Main()
    {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myAnimal.MakeSound();  // Outputs: Animal makes a sound
        myDog.MakeSound();     // Outputs: Dog barks
        myCat.MakeSound();     // Outputs: Cat meows
    }
}

Benefits of Polymorphism

  1. Code Reusability:

    • Polymorphism promotes code reuse and scalability. Base class methods can be overridden to provide specific behavior without changing the base class code.
  2. Flexibility and Maintainability:

    • It provides flexibility and makes the code easier to maintain. New classes can be added with specific implementations without modifying existing code.
  3. Interchangeability:

    • Objects can be treated as instances of their base type, allowing for more generic and interchangeable code.

Real-world Example

Consider a graphical application with different types of shapes:

public class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}

public class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}

public class Rectangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a rectangle");
    }
}

class Program
{
    static void Main()
    {
        List<Shape> shapes = new List<Shape>
        {
            new Circle(),
            new Rectangle(),
            new Circle()
        };

        foreach (Shape shape in shapes)
        {
            shape.Draw();  // Calls the appropriate Draw method based on the actual object type
        }
    }
}

In this example:

  • The Shape class has a virtual Draw method.
  • The Circle and Rectangle classes override the Draw method.
  • The shapes list contains different shapes, and the correct Draw method is called for each shape at runtime, demonstrating runtime polymorphism.

Conclusion

Polymorphism in C# is a powerful feature that allows methods to behave differently based on the object that invokes them. It enhances flexibility, reusability, and maintainability, making it a fundamental aspect of object-oriented programming. By leveraging polymorphism, developers can write more generic and adaptable code that can handle future changes and extensions gracefully.

Recent job openings