Polymorphism Interview Questions C#.NET

Polymorphism Interview Questions and Answers C#.NET

In this article, I am going to discuss most frequently asked Polymorphism Interview Questions and Answers on C#.NET. To understand the below questions and answers first have a look at the below articles where we discussed the Polymorphism in details.

Polymorphism Basics in C#

Function Overloading in C#

The Function Overriding in C#

Function Hiding in C#

What is polymorphism?

Polymorphism is one of the primary pillars of object-oriented programming. It allows us to invoke derived class methods through a base class reference variable during runtime.

In the base class, the method is declared as virtual and in the derived class, we override the same method.

The virtual keyword indicates that the method can be overridden in any derived class.

The word Polymorphism is derived from the Greek word, where Poly means many and morph means faces/ behaviors. So polymorphism means the ability to take more than one form.

Same function/ operator will show different behaviors when passed different types of values or the different number of values.

So in the simple word we can say that behaving in different ways depending upon the input received is known as polymorphism i.e. whenever the input changes automatically the output or the behavior also changes.

We can implement polymorphism in our application using three different approaches like
  1. Overloading
  2. Overriding
  3. Hiding

Overloading again is of three types

  1. Method overloading
  2. Operator overloading
  3. Constructor overloading
Explain the different types of polymorphism?

There are two types of polymorphism

  1. Static polymorphism/compile-time polymorphism /early binding
  2. Dynamic polymorphism / Run-time polymorphism /late binding
What is compile-time polymorphism?

In case of compile-time polymorphism, the object of class recognizes which method to be executed for a particular method call at the time of program compilation and binds the method call with method definition.

This happens in case of overloading because in case of overloading each method will have a different signature and basing on the method call we can easily recognize the method which matches with the method signature.

It is also called as static polymorphism or early binding.

Static polymorphism is achieved by using function overloading and operator overloading

What is runtime polymorphism?

In case of runtime polymorphism for a given method call, we can recognize which method has to be executed exactly at runtime but not in compilation time because in case of overriding and hiding we have multiple methods with the same signature.

So which method to be given preference and executed that is identified at runtime and binds the method call with its suitable method.

It is also called as dynamic polymorphism or late binding.

Dynamic polymorphism is achieved by using function overriding.

Explain different types of overloading?

Again overloading is classified into three types, such as

  1. Method overloading / Function overloading
  2. Constructor overloading
  3. Operator overloading.
What is function overloading?

Function overloading and method overloading terms are used interchangeably.

Method overloading allows a class to have multiple methods with the same name but with a different signature. So in C# functions can be overloaded based on the number, type (int, float etc) and kind (Value, Ref or Out) of parameters.

The signature of a method consists of the name of the method and the type, kind (value, reference, or output) and the number of its formal parameters. The signature of a method does not include the return type and the params modifiers. So it is not possible to overload a method just based on the return type and params modifier.

A function overloading can be compared with person overloading. If a person has already some work to do and if we are assigning additional work to the person then the person will be overloaded.

In the same way, a function will have already some work to do and if we assign a different work to the same function, then we say the function is overloaded.

It is an approach of defining multiple methods with the same method name by changing the signature.

Changing the signature means we can either change the no of parameters being passed to the method or type of parameters being passed to the method or order of parameters being passed to the function.

When should we overload methods?

To execute the same logic with different types of argument we should overload methods.

For example to add two integers, two floats and two strings we should define three methods with the same name as shown in the below application

What are the advantages of using overloading OR what are the disadvantages if we define methods with a different name?

If we overload method, the user of our application gets comfort feeling in using the method with an impression that he/she calling one method by passing different types of values.

The best example for us is “WriteLine()” method. It is an overloaded method, not a single method of taking different types of values.

When is a method considered as an overloaded method?

If two methods have the same method name those methods are considered as overloaded methods.

Then the rule we should check is both methods must have different parameter types/list/order. But there is no rule on return type, non-accessibility modifier and accessibility modifier means overloading methods can have its own return type, non-accessibility modifier, and accessibility modifier because overloading methods are different methods

Can we overload methods in the same class?

Yes, it is possible no CE, no RE. Methods can be overloaded in the same or in super and subclasses because overloaded methods are different methods.

But we can’t override the method in the same class it leads to CE: “method is already defined” because overriding methods are same methods with a different implementation.

What is the execution control flow of overloaded methods?

The compiler always checks for the called method definition in reference variable type class with the given argument type parameter.

So in searching and executing a method definition, we must consider both reference variable type and argument type

Referenced variable type for deciding from which class method should be bind.

Argument type for deciding which overloaded method should be bind.

For example:

B b = new B();

A a = new B();

b.m1(50) => b.m1(int);

=> In this method call we should search m1() method definition in B class with integer parameter at the time of program compilation and bind that method definition.

a.m1(50); => a.m1(int);

=> In this method call we should search m1() method definition in A class with int parameter not in B class even though object is B.

What is inheritance based overloading?

A method that is defined in a class can be overloaded under its child class if we overload a method in this process we call it as inheritance based overloading.

What is function/method overriding?

Redefining super class non-static method in the subclass with the same prototype is called method overriding.

The overriding method is always executed from current objects class.

In object-oriented programming method overriding is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses.

The implementation of the sub-class overrides (replaces) the implementation of superclass methods. So overridden method is always executed from the object whose object is stored in the reference variable.

Superclass method is called the overridden method and sub-class method is called the overriding method.

When must a method be overridden?

If superclass method logic is not fulfilling sub-class business requirements, the subclass should override that method with required business logic. Usually, in superclass methods are defined with generic logic which is common for all sub-classes.

When is a sub-class method treated as an overriding method?

If a method in sub-class contains the same signature as superclass non-private method then sub-class method is treated as the overriding method and superclass method is treated as the overridden method.

How can we override a parent class method under child class?

If we want to override a parent class method in its child class, first the method in the parent class must be declared as virtual by the using the keyword virtual then only the child classes gets the permission for overriding that method.

Declaring the method as virtual is marking the method is overridable.

If the child class wants to override the parent class virtual method then the child class can do it with the help of the override modifier. But overriding the method under child class is not mandatory for the child classes.

Syntax:

Class1:

Public virtual void show() //virtual function (overridable)

Class2: Class1

Public override void show() //overriding

Even if the method declared as virtual the child class may or may not override the method

In overriding parent class defines a method as virtual and gives it to child class to consume that method. So the child class now consume the method as it is or override that method as per the requirement of the child class. So overriding parent class virtual method under a child class is only optional.

How can we execute the superclass method if it is overridden in sub-class?

After re-implementing parent class methods under child class, the object of the child class calls its own methods but not its parent class method, whereas if we want to still consume or call the parent class’s methods from child class, it can be done in two different ways.

By creating a parent class object under the child class, we can call the parent class methods from the child class.

Or by using the base keyword, we can call parent class methods from child class, but this and base keyword cannot be used under the static block.

What is the difference between function overloading and function overriding?
FUNCTION OVERLOADING FUNCTION OVERRIDING
It is an approach of defining multiple methods with same name and different signature. It is an approach of defining multiple methods with the same name and same signature.
Overloading a method can be performed within a class or within the child classes also. Overriding of methods is not possible within the same class it must be performed under parent and child classes.
To overload a parent class method under child class, the child class does not require the permission from the parent. To override a parent class method under child class first the child class require an explicit permission from its parent.
This is all about defining multiple behaviors to a method. This is all about changing the behavior of a method.
Used to implement static polymorphism. Used to implement dynamic polymorphism.
This is code refinement technique. This code replacement technique.
No separate keywords are used to implement function overloading. Use virtual keyword for base class function and override keyword in derived class function to implement function overriding.
What is method hiding?

Use the new keyword to hide a base class member. We will get a compile warning if we miss the new keyword.

This is also used for re-implementing a parent class method under child class.

Reimplementing parent class methods under child classes can be done using two different approaches, such as

  1. Method overriding
  2. Method hiding

In the first case, we re-implement the parent class methods under child classes with the permission of parent class because here in parent class the method is declared as virtual giving the permission to child classes for overriding the methods.

In the 2nd approach, we re-implement the method of parent class even if those methods are not declared as virtual that is without parent permission we are re-implementing the methods.

Syntax:

Class1:

Public void display()

Class2 : Class1

Public new void display()

Using new keyword for re-implementing the methods in the child class is optional and if used will give an information to hiding.

What is the difference between Method Overriding and Method Hiding?

A parent class method can be redefined under its child class using two different approaches.

  1. Method Overriding.
  2. Method Hiding.

In Method overriding parent class gives the permission for its child class to override the method by declaring it as virtual. Now the child class can override the method using the Override keyword as it got permission from the parent.

Method hiding also parent class methods can be redefined under child classes even if they were not declared as Virtual by using ‘new’ keyword.

In method overriding a base class reference variable pointing to a child class object will invoke the overridden method in the child class.

In method hiding a base class reference variable pointing to a child class object will invoke the hidden method in the base class.

For hiding the base class method from derived class simply declare the derived class method with the new keyword.

Whereas in C#, for overriding the base class method in a derived class, we need to declare the base class method as virtual and the derived class method as the override.

If a method is simply hidden then the implementation to call is based on the compile-time type of the argument “this”.

Whereas if a method is overridden then the implementation to be called is based on the run-time type of the argument “this”.

New is reference-type specific, overriding is object-type specific.

When can a derived class override a base class member?

 A derived class can override a base class member only if the base class member is declared as virtual or abstract.

What is the difference between a virtual method and an abstract method? 

A virtual method must have a body whereas an abstract method should not have a body.

Can fields inside a class be virtual?

No, Fields inside a class cannot be virtual. Only methods, properties, events and indexers can be virtual.

Can you access a hidden base class method in the derived class? 

Yes, Hidden base class methods can be accessed from the derived class by casting the instance of the derived class to an instance of the base class as shown in the example below.

public class BaseClass
{
    public virtual void Method()
    {
        Console.WriteLine("I am a base class method.");
    }
}
public class DerivedClass : BaseClass
{
    public new void Method()
    {
        Console.WriteLine("I am a child class method.");
    }

    public static void Main()
    {
        DerivedClass DC = new DerivedClass();
        ((BaseClass)DC).Method();
    }
}

What is the difference between a virtual method and an abstract method? 

A virtual method must have a body whereas an abstract method should not have a body.

Another difference:

A Base class virtual method may or may not be overridden in the Derived class whereas a Base class Abstract method has to be implemented by the derived class.

If you want to learn Polymorphism in details in C# then please read below articles.

Polymorphism Basics in C#

Function Overloading in C#

The Function Overriding in C#

Function Hiding in C#

SUMMARY

In this article, I try to explain most frequently asked Polymorphism Interview Questions and Answers on C#.NET. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Follow Us

Leave a Reply

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