0

I have an interface, whose method could conveniently return the instance on which the method is called. For instance, a setter could return "this".

interface OriginWise<T> {
      T setOrigin( Origin origin );
}

Is there some way to get an instance of T automatically from the class it's applied to, without passing it as shown below?

class MyBean implements OriginWise<MyBean> {
    MyBean setOrigin( Origin o ){ ...; return this; }
}

Also, what's the progress with default implementations in interfaces (methods which only use other interface's methods; not the same as abstract class)?

thSoft
  • 21,755
  • 5
  • 88
  • 103
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • 1
    What do you mean "passing it in as shown below" ? The setter value is being passed into a method. The returnn object is "this". I don't grok. – NimChimpsky May 31 '13 at 14:49
  • I don't really understand the second part of your question ("Also, what's the progress..."), please, reformulate it. – thSoft May 31 '13 at 14:59
  • 1
    possible duplicate of [Is there a way to refer to the current type with a type variable?](http://stackoverflow.com/questions/7354740/is-there-a-way-to-refer-to-the-current-type-with-a-type-variable) – Paul Bellora May 31 '13 at 15:13

2 Answers2

1

default implementations in interfaces

I don't understand the rest of the question, as the code looks good to me.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

That's the best you can do. If you want to return this (which implements OriginWise), don't forget to let T extend OriginWise. See also this answer and http://passion.forco.de/content/emulating-self-types-using-java-generics-simplify-fluent-api-implementation.

Community
  • 1
  • 1
thSoft
  • 21,755
  • 5
  • 88
  • 103