C# POLYMORPHISM
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
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:
When the above code is compiled and executed, it produces the following result:
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.
When the above code is compiled and executed, it produces the following result:
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:
No comments:
Post a Comment