Examples of dynamic polymorphism in c

0 votes
I'm curious whether there are any further examples of dynamic polymorphism in C# besides method overriding. I know that method overriding is a type of dynamic polymorphism, but is this the only one?
Jun 6 in C# by rajiv
• 1,620 points
12 views

1 answer to this question.

0 votes

Check the example of polymorphism below. We have a class called 'SalaryCalculator' with a method called 'Calculate' and a parameter called 'IEmployee.' The 'Calculate' method has no idea what a 'IEmployee' is. Any class that implements the polymorphism 'IEmployee' can be used.

 class Program
{
    static void Main(string[] args)
    {
        Salarycalculator salaryCalculator = new Salarycalculator();
        salaryCalculator.calculate(new Architect());
    }


}

public class Salarycalculator
{
    public void calculate(IEmployee employee)
    {
        employee.CalculateSalary();
    }
}

public interface IEmployee
{
    void CalculateSalary();
}



public class Developer : IEmployee
{
    public void CalculateSalary()
    {

    }
}

public class Architect : IEmployee
{
    public void CalculateSalary()
    {

    }
}

}

The place receiving the object is where runtime polymorphism works, not the object initialization. 'calculate(IEmployee employee)' is an example where the 'Calculate' method only learns the type of 'IEmployee' at runtime. It can be of type 'Developer' or 'Architect,' which implies the 'Calculate' method will run polymorphically depending on the kind of object.

answered Jun 7 by pranav
• 2,320 points

Related Questions In C#

0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
19 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered 5 days ago in C# by jyoti
• 1,140 points
12 views
0 votes
1 answer

Examples of GoF design patterns in .net

I am attaching some links. Go through ...READ MORE

answered 3 days ago in C# by krishna
• 1,340 points
6 views
0 votes
1 answer

C# - Can someone please show me a very simple example of Interfaces

Take this as example interface IFlyable { ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
13 views
0 votes
0 answers

The page cannot be displayed because an internal server error has occurred on server

When  I've deployed the website I installed  ...READ MORE

May 23 in Others by Kichu
• 19,040 points
68 views
0 votes
1 answer

Object Oriented Programming Basic Concept(C#)

Use this link https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers protected internal: The type or ...READ MORE

answered Jun 17 in C# by jyoti
• 1,140 points
9 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
247 views
0 votes
1 answer

Embedding Power Bi Report Promise is not defined powerbi.js

IE does not yet support Promise. You ...READ MORE

answered Oct 30, 2018 in Power BI by Shubham
• 13,490 points
1,153 views
0 votes
1 answer

Encapsulation of subclasses from a constructor in C#

If you think about it in terms ...READ MORE

answered Jun 11 in C# by pranav
• 2,320 points
11 views
0 votes
1 answer

Which version of C# am I using

From developer cmd, type this csc -langversion:? This will ...READ MORE

answered Jun 7 in C# by pranav
• 2,320 points
10 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP