public interface Foo {
}
public class SpecificFoo implements Foo {
}
public interface SomeInterface {
void thisMethod(Foo someKindOfFoo);
}
public class SomeClass implements SomeInterface {
public void thisMethod(Foo someKindOfFoo) {
System.out.println("Dont go here please");
}
public void thisMethod(SpecificFoo specificFoo) {
System.out.println("Go here please");
}
}
public class SomeOlderClass {
public SomeOlderClass( SomeInterface inInterface ) {
SpecificFoo myFoo = new SpecificFoo();
inInterface.thisMethod(myFoo);
}
}
call code:
SomeClass myClass = new SomeClass();
SomeOlderClass olderClass = new SomeOlderClass(myClass);
I have an interface ( SomeInterface) that calls several classes (e.g. SomeOlderClass). I have a class that implements an interface, but I want to do safe-type operations for specific implementations that are passed to the common interface.
As shown in the above code, I really want to make another method that matches the specific type passed to the interface. This does not work. I guess this is because the calling code knows about the interface, not about the implementation with more specific methods (even if SpecificFoo implements Foo)
So how can I do this in the most elegant way? I can make the code work by adding an if statement to the class that implements the interface ( SomeClass):
public void thisMethod(Foo someKindOfFoo) {
if ( someKindOfFoo.getClass().equals(SpecificFoo.class) )
thisMethod(SpecificFoo.class.cast(someKindOfFoo));
else
System.out.println("Dont go here please");
}
, if, Foo. .
, SpecificFoo SomeInterface, , , SomeClass. , . ( - , , )
, , , , Foo SpecificFoo . ?
:
, , . , . , .
, RPC - GWT, , talk
Google , . , GWT-RPC bugreport , , , . (.. java, gwt javascript).
, java , .
, , Foo - , SpecificFoo - , . SomeInterface - RPC- , SomeClass - RPC . SomeOlderClass - rpc, -.
, ? , , , GWT RPC , , .