I am a newbie in Java and would like to gather some ideas, because I stucked.
I have some parent class, let's say
@Data
public class Person implements Hometown {
private String name;
private Country motherland;
}
An interface:
public interface Hometown {
Country getMotherland();
}
And some child classes:
@Data
public class Norwegian extends Person {
//motherland should be "Norway"
..
}
@Data
public class Portuguese extends Person {
//motherland should be "Portugal"
..
}
Via RestClient I can receive Person class:
public interface PopulationInfoClient {
@GET
@Path("/people/{personId}")
Person search(@PathParam("personId") String personId);
}
After I received the person information I want to set them a hometown in my service. And then return a child class of generic type (can be Norwegian, Portuguese, etc).
@Inject
@RestClient
PopulationInfoClient populationInfoClient;
public T getCitizen(String personId) {
Person person = this.populationInfoClient.search(personId);
...
return ...
}
The question is how can I create child classes like Norwegien with motherland set to "Norway" using generics? And return them within my service function getCitizen()?
I would be thankful for the answers and ideas
I tried to use Factory pattern, but failed
Well, I think you can use a custom generic factory method.
Interface:
Implementation:
Your updated code: