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, 2022 in C# by rajiv
• 1,620 points
846 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, 2022 by pranav
• 2,590 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, 2022 in C# by rajiv
• 1,620 points
677 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 Jun 21, 2022 in C# by jyoti
• 1,240 points
613 views
–1 vote
1 answer

Examples of GoF design patterns in .net

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

answered Jun 23, 2022 in C# by krishna
• 2,820 points
539 views
0 votes
0 answers

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

As the title states, what are the ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
299 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, 2022 in Others by Kichu
• 19,040 points
1,085 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, 2022 in C# by jyoti
• 1,240 points
649 views
0 votes
0 answers

How to create dependency injection for ASP.NET MVC 5

It's simple to create Dependency Injection using ...READ MORE

Jun 11, 2022 in C# by pranav
• 2,590 points
506 views
0 votes
0 answers

Object Oriented Programming Basic Concept(C#)

As We know The Default Modifier of Class, Struct, ...READ MORE

Dec 9, 2022 in Android by Edureka
• 13,620 points
474 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, 2022 in C# by pranav
• 2,590 points
681 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, 2022 in C# by pranav
• 2,590 points
468 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP