I have interface Foo
public interface Foo {
public void test();
}
Class FooChild implements interface Foo.
public class FooChild implements Foo {
@Override
public void test() {...}
public void test2() {...}
}
I have to call function test2 in somewhere in the code.
Foo f = new FooChild();
((FooChild) f).test2();
I really hate casting an interface to a class.
So I added another interface Bar.
public interface Bar extends Foo {
public void test2();
}
public class FooChild implements Bar {
@Override
public void test() {...}
@Override
public void test2() {...}
}
Now I call test2() with casting to an interface, not a class.
Foo f = new FooChild();
((Bar) f).test2();
I may add function test2 to the interface Foo,
but there are too many classes that implement Foo.
If I add the function, I have to add codes to all the classes that implement Foo.
Although I can do that above, I actually don’t have permission to edit Foo.
Will there be any fancier way to solve this problem? (hopefully without casting)
EDIT:
f is passed as a method parameter.
As Jon said, I should have be more specific.
Sorry for the confusion.
public void doSomething(Foo f) {
...
}
If you know that the variable value really has to be of type
FooChild, just declare it that way:If there’s some reason why that won’t work for you, it’s something you haven’t told us…
EDIT: Okay, so apparently it’s really a method parameter:
… so no, there’s nothing you can do to avoid the cast. The cast indicates that there’s a requirement on the value beyond what the declared variable type already guarantees – in this case, you want to say “It’s got to actually be a
FooChild, not just any implementation ofFoo.” Casts are the way of expressing that, if you can’t change the parameter.