Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • I guess in the existing heirarchy in the question it is not possible. Am I right? Commented Jul 17, 2013 at 11:10
  • The hierarchy in the question is preserved by my solution. The difference is that the Human class has an additional generic type H. Commented Jul 17, 2013 at 11:12
  • Then no, it's not possible. The type of the hair is unknown at compile time (?), so a cast is needed. Commented Jul 17, 2013 at 11:28
  • yes I think also this is the only solution, but it's like a double declaration: first Human<Blond, Bob> h = new Human<Blond, Bob>(); and second class Bob extends Person<Blond>. The link between Bob and Blond is wrote twice. And if you need to do this for many variables, not so good. Commented Jul 17, 2013 at 13:46
  • +1 for the best answer you're going to get with Java generics. Commented Jul 17, 2013 at 15:55