Home c# than ad hoc polymorphism differs from ordinary polymorphism?

than ad hoc polymorphism differs from ordinary polymorphism?

Author

Date

Category

As I understand it in C #, the polymorphism is to use virtual members and in bringing types, but it came across the mention of ad hoc polymorphism, and I can not understand what it differs from using virtual members?


Answer 1, Authority 100%

As far as I understand, AD HOC-polymorphism is a polymorphism based on the distinction of types . For various types of arguments, a different function code is used.

One Classic Example – This Overload (overloading) method:

class c
{
  Public Void F (int x) {Console.Writeline ("C.f (int)"); }
  Public Void F (String X) {Console.WriteLine ("C.f (String)"); }
}

Here the name F is tied to two different methods, and the compiler selects one or another method depending on the type of argument. This is an example static ad hoc-polymorphism: Types must be known at the compilation stage, and the compiler knows exactly which will be executed from the methods.

Another example of AD HOC-polymorphism is Redefining (overriding) of the virtual method:

class b
{
  Public Virtual Void F (int x) {Console.WriteLine ("B.f (int)"); }
}
Class D: B
{
  public override void f (int x) {console.writeline ("d.f (int)"); }
}

Here also distinguishes by type, but this time – by type of implicit zero argument This . The selection of the redefined method is postponed at the time of execution; At the time of compilation, the compiler does not exactly know which of the group of redefined methods will be completed. This is an example dynamic ad hoc-polymorphism.


In addition, there is still a concept parametric polymorphism: This is a situation where the same method works with the arguments of various types of equally, regardless of their exact type. Trivial example for languages ​​with inheritance (for example, C #) – a function working with a certain class C , can often work without changes with the object generated from C class (this type of polymorphism often called Polymorphism of inclusion ). Less trivial example is generic methods that can, depending on the generic parameter, work with different types of objects. Example:

class enumerable
{
  Public T FIRST (T & GT; SEQ)
  {
    Using (IEnumerator & lt; T & GT; EN = SEQ.geteNuMerator ())
    {
      if (! en.movenext ())
        Throw New InvalidOperationException ();
      RETURN EN.CURRENT;
    }
  }
}

Answer 2, Authority 5%

I will try to explain on the basis of the book of the GOF

ad-hock polymorphism is

  • classic (forced) polymorphism i.e., the possibility of dynamic type substitution
    (objects) during the execution of the program, but without the second form of encapsulation, i.e. without concealing the implementation (when you in the classic polymorphism make upcast, then there is a concealment of properties and methods from a derived type).

or

  • Ability to handle objects in which the same interaction interface is the same way

Example.

class a {public void method () {console.writeline ("a");}}
Class B {Public Void Method () {Console.WriteLine ("B");}}
Class C {Public Void Method () {Console.WriteLine ("C");}}

These classes do not have a base class, but they have the same interaction interface.

ad hoc polymorphism or (polymorphism for a specific case) allows you to turn in a similar way to objects that are not related to classic inheritance.

Example.

// This interface is created to create a kind of basic type for classes A, B, and C from which there is no one.
Interface IINTERFACE.
  {
    Void Method ();
  } 
// In this case, we create new classes of wrappers with similar interfaces and with a basic type
  Class Mya: a, Iinterface {}
  Class MYB: B, IInterface {}
  Class MYC: C, IInterface {}
  Class Program
  {
    Static Void Main ()
    {
// Thus, we work with classes as if they had a basic type but without concealing the implementation
        IINTERFACE INSTANCE = NEW MYA ();
        instance.method ();
        instance = new myb ();
        instance.method ();
        instance = new myc ();
        instance.method ();
     }
   }

Answer 3

There are 3 main types of polymorphism (show on the example of PHP, C # do not know):

  1. ad hoc polymorphism – several ads of functions with the same name but with different parameters, example:

When the function is called, the one corresponds to the type of argument.

Function Test (String $ A, String $ B) {
  Return $ a. $ B;
}
Function Test (INT $ A, INT $ B) {
  Return $ A + $ B;
}
// if it were supported in the PCP, it would bring
Test ('A', 'B') // 'AB'
Test (1, 4) // 5
  1. Parametric polymorphism – when the same function is performed regardless of the types of arguments.

In PHP it is supported.

Function Test (String $ A, String $ B) {
  Return $ a. $ B;
}
Function Test (INT $ A, INT $ B) {
  Return $ A + $ B;
}
// if it were supported in the PCP, it would bring
Test ('A', 'B') // 'AB'
Test (1, 4) // 5

Parametric polymorphism is a true form of polymorphism, making a language more expressive and significantly increasing the reuse coefficient of code.
Traditionally, he is opposed to ad hoc polymorphism (imaginary form).

  1. Subtype polymorphism – as far as this is the most popular concept of polymorphism.

This is the ability of objects with the same specification to have a different implementation.

For example, there is a LoggerInterface interface, it is as a specification. If there are classes that implement this interface, for example, the class FileLogger and Dblogger, then this is called polymorphism.

interface loggerinterface {
 Public Function Log ($ Logs);
}
Class Filelogger Implements LoggerInterface {
 public function log ($ logs)
 {
  // writes logs to the file
 }
}
class DBLogger implements LoggerInterface {
 Public Function Log ($ Logs)
 {
  // logs into the database
 }
}

Instead, the interface can be inheritance through the abstract (virtual) class or an ordinary class.

That is, when there are methods in different classes that do the same thing,
they can be given the same name and to make these classes implemented a common interface
or inherit a single abstract class from this abstract method.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions