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.
getDirectHair()is about inheritance.