3 ms·
I suppose it might be "bad form", but couldn't you, if you had: public interface FooBar { int foo(); int bar(); } public Afob implements FooB
by e12e 13y ago
I suppose it might be "bad form", but couldn't you, if you had:
public interface FooBar
{
int foo();
int bar();
}
public Afob implements FooBar {...}
refactor FooBar to simply:
public interface Foo { int foo() }
public interface Bar { int bar() }
public interface FooBar extends Foo, Bar {}
And keep FooBar the way it was, but now cast it to a Foo (or Bar) when you don't want a FooBar?
- pron 13y agoThe problem is with interface FooBar extends Foo, Bar {} FooBar can be defined in a library that you can't change. The solution in Java 8 would be using default methods: interface Foo extends FooBar { default int bar() { throw new UnsupportedOperationException(); } } And then you can even do the other trick mentioned in the article even more easily: Foo f = () -> 3; And f now satisfies FooBar, by throwing an exception when bar is called, and returning 3 when foo is called. But, assuming method void doSomething(FooBar x) { ... } you can't call it like this: doSomething(() -> 3); What you can do is call it like this: doSomething((Foo)() -> 3);