0

I have a legacy interface that looks like :

/** Never change *a line* of this or some angry monster will fall from the opening skies talking obscure reasons not to mess with this */
public interface IllFindStuffNoWorries<STUFF> {
  STUFF findById(String id);
  STUFF findById(Long id);
  STUFF findById(UUID id);
  ... and so on
}

(Which is pretty inconvenient since I never know what type of id it expects) But it turns out all the STUFF of the project has a method getId() that returns one of the types that the implementation-associated IllFindStuffNoWorries is willing to handle.

So I introduced a:

public interface IveGotId<ID> { ID getId(); }

Which is now extended by all my STUFF-s, allowing me to introduce:

public interface TypicalFinder<STUFF extends IveGotId<ID>, ID> {  //get it ? typical because types ... ha ha that sounded hilarious in my head ...
  STUFF findById(ID id);
}

So I had a Something SomethingFinder which I modified from:

/** Change as little as you can there  */
public class Something {
  ...
  String getId(){ return id; }
  ...
}
public interface SomethingFinder extends IllFindStuffNoWorries<Something> {}

To:

/** Change as little as you can there  */
public class Something implements IveGotId<String> {
  ...
  @Override String getId(){ return id; }
  ...
}
public interface SomethingFinder extends IllFindStuffNoWorries<Something>, TypicalFinder<Something, String> {}

While this would have been fine in java 7- (according to years old memories), compiling it with java8(+?) yields me this :

/home/stackOverflowUserAr3s/projects/hot-mess/src/main/java/some/where/impl/SomethingFinderImpl.java:12:42
java: reference to findById is ambiguous
  both method findById(ID)                in new.mess.TypicalFinder
   and method findById(java.lang.String)  in old.mess.IllFindStuffNoWorries
match

Is there a way to make this work ?
Am I trying to rely on an outdated thinking ?
Are @FunctionnalInterface-s a sort of way to go in my case (just one method that interests me) ?
Those are interfaces, extended by an interface ... This is not some funky intent of multiple-implementations, that is a conjunction of contracts that conveniently overlap and can both be satisfied by a single implementation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ar3s
  • 2,237
  • 2
  • 25
  • 47
  • Is it the same case as https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method ? – Cédric Rup Feb 18 '21 at 16:01
  • Not really no, in the other question @Jothi wonders which interface will see it's method overridden (which, in his pre-8 context, is/are both), in my case (8+) I'm having trouble when implementing `SomethingFinder ` I get a message telling me it's illegal and how naughty I am ... (which I don't fell like) – Ar3s Feb 18 '21 at 16:29
  • 1
    You didn’t show what `SomethingFinderImpl` does. – Holger Feb 18 '21 at 17:34
  • @Holger : It did not strike me as very relevant (it just implements `SomethingFinder`). Could it be ? – Ar3s Feb 18 '21 at 22:07
  • 1
    Really? You get a compiler error in `SomethingFinderImpl.java:12:42` and think, it’s not relevant what happens there? Even the information you now gave in the comment, “it just implements `SomethingFinder`”, was not available to any reader before. – Holger Feb 19 '21 at 07:26
  • Really, yes, I was not being sarcastic, I got a feeling you took it that way and I'd like to, first, apologize for that. I genuinely thought that the code of `SomethingFinderImpl` and detailing that it implements `SomethingFinder` was redundant because the message `... reference to findById is ambiguous ...` would suffice. And even though I still think it _is redundant_ in a way I really must say that my post would benefit from this extra context. I kept my nose deep down what is (I think you'd have guessed) a complicated and unforgiving project. I'll edit my Q. – Ar3s Feb 19 '21 at 08:44
  • in addition to my previous comment announcing an edit, I found an explanation of *[A]* What is happening *[B]* How I could go about in my context (which is less relevant to this post but still) *[C]* Some misconceptions and false memories I had *[Conlusion]* this edit is going to be massive and for _deadline-meeting-willingness_ resaons it might take a while – Ar3s Feb 19 '21 at 08:50

0 Answers0