Delegation (object-oriented programming)

This sense of delegation as programming language feature making use of the method lookup rules for dispatching so-called self-calls was defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems".

Delegation is dependent upon dynamic binding, as it requires that a given method call can invoke different segments of code at runtime[citation needed].

It has been argued that delegation may in some cases be preferred to inheritance to make program code more readable and understandable.

The precise relationship between delegation and inheritance is complicated; some authors consider them equivalent, or one a special case of the other.

Inheritance, by contrast, typically targets the type rather than the instances, and is restricted to compile time.

Here is a pseudocode example in a C#/Java like language: Calling b.foo() will result in b.bar being printed, since this refers to the original receiver object, b, within the context of a.

Using inheritance, the analogous code (using capital letters to emphasize that resolution is based on classes, not objects) is: Calling b.foo() will result in B.bar.