0

I am trying to implement a fluent API that contains specialized derived classes and I am using the Derived extends Base<Derived> method to obtain this parameter as Derived from base methods, but it doesn't seem to work with multiple generic parameters, for instance:

public class Specialized<T> extends Base<Specialized<T>, Iterable<T>> {
    void someAMethod() {
    }
}

public class Base<ThisType extends Base<ThisType, T>, T> {
    public ThisType baseMethod() {
        return getMe();
    }

    @SuppressWarnings("unchecked")
    public ThisType getMe() {
        return (ThisType) this;
    }
}

And later use it like:

new Specialized().baseMethod()
                 .someAMethod();

But baseMethod() doesn't return a Specialized object, so I am getting this error:

Cannot resolve method 'someAMethod()'

Jezor
  • 3,253
  • 2
  • 19
  • 43
  • 1
    You're using a raw type in `new Specialized()...`, this will erase all generics, so the method will return a `Base` object, since that is it's erasure. – Jorn Vernee Dec 17 '16 at 19:43
  • See [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Jorn Vernee Dec 17 '16 at 19:43
  • @Jean-FrançoisSavard wopsie, I was testing it as a nested class in test class, so `public` wasn't really necessary. – Jezor Dec 17 '16 at 23:37
  • @JornVernee you're right, eh, how could I not notice that? Thank you! – Jezor Dec 17 '16 at 23:37

0 Answers0