Polymorphism Interview Questions

Below is the list of Polymorphism Interview Questions for interview preparations for beginners and experience professional

What is Polymorphism in C#?

Polymorphism is one of the main pillars of object-oriented programming (OOP), it allows the programmer to change the object behavior based on object creation or way the is being called.

It also provides the facility to inject different features to the existing functionality or operators by overriding the existing behaviors.

It provides three different approaches to change the behavior the same functions

  • Overloading
  • Overriding
  • Hiding

To understand more about polymorphism please read the tutorial of Polymorphism.


Static polymorphism vs Dynamic polymorphism

polymorphism interview questions, polymorphism interview questions for experienced in c#, polymorphism interview questions c++, java interview questions, abstraction in java interview questions, polymorphism in java, encapsulation in java interview questions, java polymorphism tricky questions

What is method hiding?

If a derived class defines a class method with the same signature as a class method in the base class. The method in the derived class hides the one in the base class.

Method hiding means the derived class has defined a method with the same signature as a class method in the base class. In that case, the method of the base class is hidden by the derived class.

So, the derived class method execution is determined by the object that is used to invoke it. In fact, it will be determined by the type of reference variable used to invoke the method.
In C# we use a “new” keyword to hide the method of the derived class read more about method hiding.


How can we override a base class method under derived class?

To override the base method into a derived class, the first base class method should be declared with a ‘virtual’ keyword. Then only the base class methods can override the parent method which is declared using the ‘virtual’ keyword.

public class Car
{
    public virtual void Speed()
    {
        Console.WriteLine("Speed is 60kmh");
    }
}


public class Audi : Car
{
    public override void Speed()
    {
        Console.WriteLine("Speed is 120kmh");
    }
}

When we declare the ‘virtual’ method in the base class, then the base class declares that the method can be implemented based on derived class implementation.

Points to remember

  • An only virtual or abstract method can be overridden
  • Even if the base class method declared as virtual then also derived class may or may not override the method.

How to execute the base class method if it is overridden in the derived class?

If we want to call the base class method using derived class instance then to call the base class method, we can use ‘base’ keyword to call the base class method.

public class Car
{
    public virtual void Speed()
    {
        Console.WriteLine("Speed is 60kmh");
    }
}


public class Audi : Car
{
    public override void Speed()
    {
        base.Speed(); //it will call parent class Speed() method
    }
}

Also, we can call base class method from derived class at any time.


How to access a hidden base class method in the derived class?

We can access the hidden base class method from derived class instance by casting the instance of the derived class to the base class.
For example in the below code snippet we have ‘Car’ class as the base class and ‘Audi’ class as a derived class, so we have cast ‘Audi’ class instance to ‘Car’ class to access the base class hidden method

class Program
{

    static void Main()
    {
        Audi car = new Audi();
        ((Car)car).Speed();

        //car.Speed();
        Console.ReadLine();
    }
}

public class Car
{
    public virtual void Speed()
    {
        Console.WriteLine("Speed is 60kmh");
    }
}


public class Audi : Car
{
    public new void Speed()
    {
        Console.WriteLine("Speed is 120kmh");
    }
}

What is output of below program?

class Program
{

    static void Main()
    {
        Car car = new Audi();

        car.Speed();
        Console.ReadLine();
    }
}

public class Car
{
    public virtual void Speed()
    {
        Console.WriteLine("Speed is 60kmh");
    }
}


public class Audi : Car
{
    public new void Speed()
    {
        Console.WriteLine("Speed is 120kmh");
    }
}

Output of the above code snippet

Speed is 60kmh

As ‘Audi’ class is derived from ‘Car’ class, and derived class speed() method is hidden, However, we create an instance of the derived class as the method is hidden it will execute the base class method.


What is the output of below program? What is the importance of new keyword?

class Program
{
    static void Main()
    {
        Car car = new Audi();

        car.Speed();
        Console.ReadLine();
    }
}

public class Car
{
    public virtual void Speed()
    {
        Console.WriteLine("Speed is 60kmh");
    }

    public virtual void EnginePower()
    {
        Console.WriteLine("70kW");
    }
}

Output of the above code snippet

Speed is 60kmh
70kW

In both cases, base class methods will be executed as the derived call method has been hidden. if base class and derived class both contain methods with the same name and if we use a base reference with child object then base class methods will be always called.
The above program gets executed with a warning message. As the compiler can see the method is hidden but the compiler is unable to understand whether the hidden method is intended or not.
To specify the hidden intention of a method we use ‘new’ keyword in C#, it specifies that method hiding is international.


Why we need to overload methods?

Sometimes we need to execute the same logic should be executed with a different signature. For example, to add two integers, two string, and two doubles we should define three methods with the same name as shown in the below code snippet.

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

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

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

Also, sometimes we need to execute some of the class with different values so we an overloaded constructor too.


Read more about Polymorphism and Online Free Polymorphism Multiple Choice Question Quiz for Interview preparations. also, you can check Inheritance interview questions and read more about abstraction, encapsulation for interview preparation.

Add comment if you have any Polymorphism Interview Questions.

Leave a Reply

Your email address will not be published. Required fields are marked *