Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>



Polymorphism means that the words have different forms. In object-oriented programming paradigm, polymorphism is often expressed as ' a single interface, some of the functions.

Polymorphism can be static or dynamic. In static polymorphism, the response to the specified function at compile time. In the dynamic polymorphism, decided at run time.

Static polymorphism

The mechanism connects a function with the object during the time of the compilation called early binding. It is also called static glue. C # provides two techniques for implementing static polymorphism. Are:
  • Function overloading
  • Operator overloading
We discuss operator overloading in the next chapter.

Function overloading

You can have multiple definitions for the same function name within the same scope. Function definition must be different from each other for the type and/or the number of arguments in the argument list. Different functions can be overloaded only statement with the return type.

The following example demonstrates using the function print() to print the data type:

Polymorphism Sample Project

When the above code is compiled and executed, it produces the following result:

Polymorphism Result Project Example

Dynamic polymorphism

C # enables you to create Abstract classes that are used to provide partial classes that implement the interface. Implementation is complete when the Class inherits from it. The abstract class contains an abstract method, which is implemented by the derived class. Class derivation has more specific functionality.

Here are the rules about abstract classes:
  • You cannot create instances of an abstract class
  • You cannot declare an abstract method abstract outside the classroom
  • When a class is declared closed, cannot be inherited, non-abstract class is declared sealed.
The following program shows the abstract class:

Example Project Abstract Class in Polymorphism

When the above code is compiled and executed, it produces the following result:

Result of Example Project Abstract Class

When you have a function that is defined in the class that you want to implement in legacy class (es), a virtual function that you use. Virtual functions can be implemented differently in different class inheritance and calls to these functions will be determined at run time.

Dynamic polymorphism implemented from the abstract classes and virtual functions.

The program below demonstrates this:

When the above code is compiled and executed, it produces the following result:

This post first appeared on GNIITSolution, please read the originial post: here

Share the post



Subscribe to Gniitsolution

Get updates delivered right to your inbox!

Thank you for your subscription