0


Does anyone can tell me how to code the return in the function "getDirectHair()" ?
I want to create a method which is like a shortcut in the Human class to directly return the good type of Hair class (below named "h.getDirectHair()") instead of use "h.getPerson().getHair()".
I want to use the type <?> of Person<?> declared in Human class.

package test; public class Test { public static void main(String[] args) { Human<Bob> h = new Human<Bob>(); Blond blond = h.getPerson().getHair(); // no cast needed, because Human<Bob> is blond //how to do if I want to use directly this : blond = (!!!) h.getDirectHair(); //need cast !! Blond or Brown ? } } class Human<T extends Person<?>>{ private T person = null; public T getPerson() { return person; } public /* <?> */ Object getDirectHair(){ // => I want to return the type <?> of Person<?> // instead of Object, how to ?? return person.getHair(); } } class Person<T extends Hair> { T hair; public Person(T hairr) { hair = hairr; } public T getHair() { return hair; } } class Bob extends Person<Blond> { public Bob(Blond bean) { super(bean); } public Blond getHair() { return super.getHair(); } } class Barack extends Person<Brown> { public Barack(Brown bean) { super(bean); } public Brown getHair() { return super.getHair(); } } class Hair { } class Blond extends Hair { } class Brown extends Hair { } 


Many thanks and best regards,
David.

5
  • It's "heir", not "hair", BTW :). Commented Jul 17, 2013 at 10:54
  • @TheTerribleSwiftTomato: not if you read the examples Commented Jul 17, 2013 at 10:55
  • @TheTerribleSwiftTomato I assume Hair is correct, as getHair return Blond and Brown Commented Jul 17, 2013 at 10:56
  • @user2590746 why do you want to use generics for this? it seems like the ideal case of inheritance, Bob extends Person, Person extends Human, Human has getHair Commented Jul 17, 2013 at 10:59
  • Ah, my bad, read too quickly and assumed getDirectHair() is about inheritance. Commented Jul 17, 2013 at 11:31

1 Answer 1

4

Here's how to fix the problem:

public class Test { public static void main(String[] args) { Human<Blond, Bob> h = new Human<Blond, Bob>(); Blond blond = h.getPerson().getHair(); blond = h.getDirectHair(); } } class Human<H extends Hair, T extends Person<H>>{ private T person = null; public T getPerson() { return person; } public H getDirectHair(){ return person.getHair(); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

I guess in the existing heirarchy in the question it is not possible. Am I right?
The hierarchy in the question is preserved by my solution. The difference is that the Human class has an additional generic type H.
Then no, it's not possible. The type of the hair is unknown at compile time (?), so a cast is needed.
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.
+1 for the best answer you're going to get with Java generics.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.