I am working with a 3rd party framework and it turns out I need to wrap some of its objects as a delegate to one of my classes.
class Foo { // 3rd party class.
protected void method() {}
}
class FooWrapper extends Foo {
private Foo mDelegate;
public FooWrapper(Foo inDelegate) {
mDelegate = inDelegate;
}
protected void method() {
mDelegate.method(); // error can't access protected method() of mDelegate
}
}
So there is the problem. I need to delegate this method to the internal object but its protected and therefore not accessible.
Any ideas on ways to solve this particular problem? This is for Java 1.3.
Why are you constructing a separate instance of Foo? FooWrapper is already a Foo.
Edit: if you really have to have separate instance, one way (albeit slightly ugly) is to use reflection:
Edit2: Based on your comment below, have two sub-classes of Foo, one that simply provides public versions of the protected methods, and one that has overrides all of the important methods. The first will look and act exactly like a Foo, the second can do whatever you need doing: