Learn C# oops Polymorphism

by | Sep 29, 2023 | C# OOP | 0 comments

Polymorphism

Simplifying Polymorphism in C# with Examples

Polymorphism is a fundamental concept in object-oriented programming (OOP), and it can be understood more easily by breaking it down into its key components.

What is Polymorphism?

At its core, polymorphism means “many forms.” In the context of OOP, it allows you to work with different classes in a way that treats them as instances of a common base class. This makes your code more adaptable and versatile.

Two Types of Polymorphism

Polymorphism can be categorized into two types: static and dynamic.

Static Polymorphism (Compile-Time Polymorphism)

In static polymorphism, the decision about which function to use is made at compile time. It is also known as early binding. C# offers two techniques to implement static polymorphism: function overloading and operator overloading.

Function Overloading

Function overloading enables you to have multiple definitions of the same function name within the same scope. The definitions must differ by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.

Here’s an example demonstrating function overloading:

Examples-

Example 1: Function Overloading with Different Parameter Types

In this example, we have overloaded the Add method to work with both integers and doubles. Depending on the argument types, the appropriate version of the method will be called.

Example 2: Function Overloading with Different Number of Parameters

Here, we have two Add methods that accept different numbers of parameters. You can call the appropriate method based on how many values you want to add together.

Tricky Question 1: What happens if you have two overloaded methods with the same parameter types but different return type?

Consider the following code:

Solution: This will result in a compilation error because C# does not allow function overloading based solely on the return type. The compiler cannot differentiate between these two methods based on the parameter types alone, as both methods take two integers.

Tricky Question 2: What happens when you have overloaded methods with optional parameters?

Solution: When calling the Add method, if you provide two arguments, the first version of the method (with two parameters) will be called. If you provide three arguments, the second version (with three parameters) will be called. The optional parameter c with a default value of 0 allows for this behavior.

Tricky Question 3: Can you overload a method by just changing the return type?

No, as addressed before by an example, you cannot overload a method in C# solely by changing its return type. The compiler does not consider the return type when resolving which overloaded method to call. It relies on the method name and the parameter list to determine the appropriate method.

Tricky Question 4: What happens if there is an ambiguity between overloaded methods?

Consider this code:

Solution: In this case, you will get a compilation error because there is an ambiguity between the two methods. Both methods have the same name and the same parameter types, so the compiler cannot determine which one to call based on the method signature alone. You should rename one of the methods to resolve the ambiguity.

Function overloading is a powerful feature in C#, but you should be careful to ensure that overloaded methods have distinct parameter lists to avoid ambiguity and compilation errors.

B – Dynamic Polymorphism (Run-Time Polymorphism)

Dynamic polymorphism, on the other hand, determines which function to use at runtime. It is also known as late binding. This type of polymorphism is achieved through abstract classes and virtual functions.

Example: Dynamic Polymorphism Virtual Override Keywords

What is Virtual

We use this to mark function virtual, so it can be override in base class to achieve runtime polymorphism.

What is Override

Use in child class method, which we are going to override from base.

In this example, we’ll create a hierarchy of shapes with a base class Shape and derived classes Circle and Rectangle. We’ll use dynamic polymorphism to demonstrate how method overriding works.

Output:

In this example, we create an array of Shape objects and assign instances of Circle, Rectangle, and Shape to it. When we call the Draw method on each shape, dynamic polymorphism ensures that the correct version of the Draw method is called based on the actual type of the object using Virtual and Override keywords.

0 Comments

Related Posts

Delegates in C#: Complete tutorial

Learn Delegates c# In C#, a delegate is a type that represents references to methods with a particular parameter list and return type. Delegates provide a way to encapsulate a method, and they are often used for defining callback methods and implementing event...

Inheritance in C# oops complete guide

Inheritance Table of content: Topics1Why i wrote this article2Inheritance - complete guide3All possible scenarios based/ tricky in/out type of Interview questions Why i wrote this article? When I was preparing c#/oops, the main challenge was information was scattered...

Index:C# OOP

Delegates in C#: Complete tutorial

Learn Delegates c# In C#, a delegate is a type that represents references to methods with a particular parameter list and return type. Delegates provide a way to encapsulate a method, and they are often used for defining callback methods and implementing event...

Inheritance in C# oops complete guide

Inheritance Table of content: Topics1Why i wrote this article2Inheritance - complete guide3All possible scenarios based/ tricky in/out type of Interview questions Why i wrote this article? When I was preparing c#/oops, the main challenge was information was scattered...