Consider the following class that defines and implements the Foo
interface:
public class MyClass {
public Foo getFoo() {
return new FooImpl();
}
public void fooMethod(Foo foo) {
final fooImpl = (FooImpl) foo;
fooImpl.hiddenMethod();
}
public interface Foo {
void doSomething();
}
private class FooImpl implements Foo {
public void doSomething();
void hiddenMethod() {
}
}
}
Outside classes will call MyClass.getFoo()
to obtain a Foo
object and may pass it back to fooMethod()
. fooMethod()
expects the FooImpl
implementation of Foo
and casts Foo
to FooImpl
so it can call package-private method hiddenMethod()
.
My question is, I'm not crazy about casting Foo
to FooImpl
. Is there another pattern I can use to do the same thing?
Thanks in advance...