Dynamic dispatch

In computer science, in particular object-oriented programming, dynamic binding (English dynamic binding / dynamic dispatch ) is a term that describes how the compiler with polymorphic methods.

This is called dynamic binding, when a method call is resolved at run time based on the actual ( dynamic ) type of an object.

Explanation

In class hierarchies can be a variable whose type is set to a certain class, also instances of its subclasses include, where possible methods of the superclass have been overwritten.

If you call now for the object stored in the variable a particular method, there are two candidates:

From dynamic binding is when in such cases at run- time depending on the fact whether the object is an instance of the superclass or a subclass, the version of the method is called. If, instead, always the base class method used, it is called static binding. Many object-oriented programming languages ​​allow you to specify individually for each method, whether static or dynamic binding is to be applied. This property is mitvererbt.

The dynamic binding is of enormous importance for object-oriented programming, since the flexibility of inheritance only through dynamic binding takes effect.

Example

A typical example for dynamic binding in Java:

Class Mammal {      Fact file: void () {        System.out.println ( "I am a mammal. ");      }    }      class Cheetah extends Mammal {      Fact file: void () {        System.out.println ("I 'm a cheetah. ");      }    }      Elephant class extends Mammal {      Fact file: void () {        System.out.println ("I am an elephant. ");      }    }      public class Example { Dynamic binding      public static void main ( String [ ] args ) {        java.util.List animals = new java.util.ArrayList ();        tiere.add (new Mammal ());        tiere.add (new Cheetah ());        tiere.add (new Elephant ());          for ( mammal animal: animals ) {          tier.steckbrief ();        }      }    } output I am a mammal. I'm a cheetah. I am an elephant.

Implementation

The dynamic binding requires type checking at runtime. It must therefore extend the source code and leads to a ( slight ) slowdown of the program. Usually, the compiler creates for each virtual method is a table of virtual methods, which contains references to the invoked methods for the possible dynamic types.

  • Object-Oriented Programming
250183
de