In situations where two interfaces apply to an object, and there are two overloaded methods that differ only by distinguishing between those interfaces, which method gets called?
In code.
interface Foo {}
interface Bar {}
class Jaz implements Foo, Bar {}
void DoSomething(Foo theObject)
{
System.out.println("Foo");
}
void DoSomething(Bar theObject)
{
System.out.println("Bar");
}
Jaz j = new Jaz();
DoSomething(j);
Which method will get called? DoSomething(Foo) or DoSomething(Bar)? Neither is more specific than the other, and I see no reason why one should be called instead of the other, except that one is specified first/last.
EDIT: And with this type of code is it possible to force one or the other method?
I'm pretty sure the above won't compile. DoSomething(j)
is an ambiguous call and will result in an error.
To get it to compile, you'd have to specifically cast j
as a Foo
or Bar
when you call DoSomething
, for example DoSomething((Foo)j)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With