In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as IMyInterface1 or IMyInterface2. Is this possible in Java?
4 Answers
No, there's nothing like C#'s explicit interface implementation in Java.
On the plus side, Java has covariant return types, so if you want to provide a more strongly typed implementation than the interface specifies, that's okay. For instance, this is fine:
interface Foo
{
Object getBar();
}
public class Test implements Foo
{
@Override
public String getBar()
{
return "hi";
}
}
C# wouldn't allow that (prior to C# 9, which now supports covariant return types) - and one of the ways around it is typically to implement the interface explicitly and then have a more specific public method (usually called by the interface implementation).

- 1,421,763
- 867
- 9,128
- 9,194
-
P.S. - "C# wouldn't allow that" is not the case anymore, C# 9.0 supports covariant return types :) – Kir_Antipov Jul 29 '21 at 15:49
-
@Kir_Antipov: Yes, answers from 12 years ago will often be outdated :) – Jon Skeet Jul 29 '21 at 16:05
-
1Yeah, I know xD On the contrary, Java-part of the answer is still valid, so I thought it's worth to mention the fact that at least one of the languages is actually developing :) – Kir_Antipov Jul 29 '21 at 16:52
You can achieve similar effect using the mechanism of anonymous interface implementation in Java.
See example:
interface Foo {
void f();
}
interface Bar {
void f();
}
public class Test {
private String foo = "foo", bar = "bar";
Foo getFoo() {
return new Foo() {
@Override
public void f() {
System.out.println(foo);
}
};
}
Bar getBar() {
return new Bar() {
@Override
public void f() {
System.out.println(bar);
}
};
}
public static void main(String... args) {
Test test = new Test();
test.getFoo().f();
test.getBar().f();
}
}

- 41
- 1
You can only do this if the methods are overloaded. If you have two method which are expected to do different things, they should have different names IMHO.

- 525,659
- 79
- 751
- 1,130
No and it should never be present in Java. It's just another bone to throw at people who can't be bothered with good design.
Explicit implementation of an interface should never be needed or used. There are better ways to solver the problem that this tries to solve.

- 1,192
- 1
- 13
- 32
-
1Do you also believe that private inheritance is only for "people who can't be bothered with good design"? That's another useful OOP capability missing from Java. – Ben Voigt May 21 '10 at 04:41
-
It will never be in Java for the same reason that delegates will never be in Java.. google it. There is a good article about why delegates where never done in Java and this is similar. It's bad because of what it allows you to do not the good stuff you can do with it. Something Microsoft and it's ecosystem don't get. – eaglestorm May 24 '10 at 00:20
-
2@ealgestorm, you do realize that even Swing "abuses" objects to make them behave like delegates, right? Also, I do find it a bit amusing that almost all of your questions are related to .NET. – Teo Klestrup Röijezon Jul 12 '11 at 17:04
-
@DontCare4Free lol you should read up on why java was designed that way, i think delegates are one of the things that are a bad design choice in .NET - it leads to bad design because of what you can do with them. – eaglestorm Jul 13 '11 at 00:08
-
@DontCare4Free i do what I get paid at work to do not what I like doing – eaglestorm Jul 13 '11 at 01:44
-
I'm not eating any words and it makes me very nervous about the future of Java, I do like lambda expressions as they are very convenient (not always good though) but I hope they restrict how closures can be used so that it encourages good software design and doesn't break the swing event model. – eaglestorm Nov 10 '11 at 00:26
-
1@eaglestorm, no offence, but I have a little experience with java now, being a .NET developer, and I find anonymous interface implementations very close to what I used to do with delegates. I event believe anonymous interfaces are more powerful in some cases, so, IMHO I do not agree that delegates are bad practice, and that they are not present in Java. – Ivaylo Slavov Jan 09 '12 at 18:15
-
2As for explicit interfaces - in complex scenarios you might happen to conform to the APIs of two distinct systems, which happen to define interfaces with matching methods, and still must have different implementations. The burden of complications obviously comes to the consuming code. – Ivaylo Slavov Jan 09 '12 at 18:19
-