The disadvantages of wrapper classes are few. One caveat is that wrapper classes are not suited for use in callback frameworks, wherein objects pass self references to other objects for subsequent invocations (“callbacks”). Because a wrapped object doesn’t know of its wrapper, it passes a reference to itself (this) and callbacks elude the wrapper.
Can someone explain what this means with an example perhaps. It is written in Effective Java but I did not completely understand it.
To add to the context, instead of inheritance we should favor composition which leads to instead to sub-classing Set
we should use something like this:
public class ForwardingSet<E> implements Set<E> {
private final Set<E> s;
public ForwardingSet(Set<E> s) { this.s = s; }
public void clear() { s.clear(); }
public boolean contains(Object o) { return s.contains(o); }
...
}
But, how this will fail, I am still not able to understand for callbacks. In JavaScript we can use function callbacks but how the same concept applies to Java if someone can explain.