Instantiate right class depending on Locale value

I would like to know if it’s possible to do what I want.
I have a Locale value, and depending on its value, I would like to instantiate the right class automatically only with the parent’s constructor.

For example: I would like to do this

void aMethod()

void doSomethings()
     Parent parent = new Parent();

And based on a Locale, for example Locale.France. I would like to have ChildFR to be instantiated when I call new Parent().

Please let me know if I was clear enough, or if it’s possible with Java. Every idea is welcome.

You may want to look at the Factory Pattern or the Builder Pattern (whose primary goal is not helping with polymorphism but may does so) .

They don’t do what you want to do which is instantiating a child by calling the parent constructor but they are solutions to what you’re trying to accomplish (instantiating relatively to some state or configuration).

For both patterns, you need to define an abstraction of your implementation (e.g. Parent) and the concrete implementations of the abstraction (e.g. your children like ChildFR). Then the instantiating job is delegated to your Factory or Builder which will return an instance of one of your concrete implementations relatively to your variable.

You’ll have your factory method signature and matching call like:

// return instance relatively to value of locality parameter
CustomFactory.create(String locality) : Parent 

Parent parent = (new CustomFactory()).create(var);

or your builder method signatures and matching call like:

// set locality
CustomBuilder.locatility(String locality) : CustomBuilder
// return instance relatively to value of locatily parameter : Parent

Parent parent = (new CustomBuilder()).locality(var).build();