1

I have these three Interfaces as part of the Fluent Interface API (where a lot of method chaining happens):

    interface VerifierA extends VerifierC {

      VerifierA method1();
      // other methods elided
    }

    interface VerifierB extends VerifierC {
     VerifierB method1();
       // other methods elided
    }

   interface VerifierC {
   VerifierA commonMethod();
   }

By declaring commonMethod() as VerifierA I can then chain methods commonMethod().method1() but not commonMethod().method2();

How can I have commonMethod() in VerifierC return to both VerifierA or VerifierB as needed?

Andrejs
  • 10,803
  • 4
  • 43
  • 48
  • 1
    I think this is already asked in http://stackoverflow.com/questions/1069528/method-chaining-inheritance-don-t-play-well-together or http://stackoverflow.com/questions/9655335/method-chaining-how-to-use-getthis-trick-in-case-of-multi-level-inheritance – pringi Jan 25 '17 at 10:17
  • Having read through the referenced answer, I'm afraid I'm unable to apply it to my situation consisting of only interfaces... – Andrejs Jan 25 '17 at 10:32
  • Why you not simply let commonMethod return VerifierC and override in VerifierB to return VerifierB and VerifierA to return VerifierA? – david Jan 25 '17 at 10:45

1 Answers1

2

you could solve it with generics:

interface VerifierA extends VerifierC<VerifierA> {
     VerifierC<VerifierA> method1();
}

interface VerifierB extends VerifierC<VerifierB> {
    VerifierC<VerifierB> method1();
}

interface VerifierC<T> {    
    T commonMethod();
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135