answersLogoWhite

0

Delegation allows the behavior of an object to be defined in terms of the behavior of another object.

ie, Delegation is alternative to class inheritance. Delegation is a way of making object composition as powerful as inheritance. In delegation, two objects are involved in handling a request: a receiving object delegates operations to its delegate. this is analogous to the child classes sending requests to the parent classes.

Example in a Java/C# like language class A {

void foo() {

this.bar() // "this" is also known under the names "current", "me" and "self" in other languages

}

void bar() {

print("a.bar")

}

}

class B {

private A a; // delegationlink

public B(A a)

{

this.a = a;

}

void foo() {

a.foo() // call foo() on the a-instance

}

void bar() {

print("b.bar")

}

}

a = new A()

b = new B(a) // establish delegation between two objects

Calling b.foo()

will result in a.bar being printed, since class B "delegates" the method foo() to a given object of class A.

User Avatar

Wiki User

15y ago

What else can I help you with?