FeatureMap contains features, derived classes have some mandatory features that the map should have, say keys (the mandatory part) are added and all the features are fetched from somewhere. Since clients interact with FeatureMap, a featuremap needs to be returned and the type is part of the actual object. Is there a better way to create instead of createFeatureMapWithMandatoryValuesimport lombok.NonNull;import java.util.Collections;import java.util.HashMap;import java.util.Map;public abstract class FeatureMap {    private Map<String, Object> features;    public FeatureMap() {        features = new HashMap<>();    }    void addFeature(@NonNull String name, @NonNull Object value) {        features.put(name, value);    }    abstract FeatureMap createFeatureMapWithMandatoryValues(Map<String, Object> values);}class FoodFeatureMap extends FeatureMap {    public static final String foodType = foodType;    public FoodFeatureMap(@NonNull String foodTypeValue) {        addFeature(foodType, foodTypeValue);    }    @Override    FeatureMap createFeatureMapWithMandatoryValues(Map<String, Object> values) {        return new FoodFeatureMap((String) values.get(foodType));    }}class HumanFeatureMap extends FeatureMap {    public static final String name = name;    public static final String origin = origin;    public HumanFeatureMap(@NonNull String nameValue, @NonNull String originValue) {        addFeature(name, nameValue);        addFeature(origin, originValue);    }    @Override    FeatureMap createFeatureMapWithMandatoryValues(Map<String, Object> values) {        return new HumanFeatureMap((String)values.get(name), (String) values.get(origin));    }}class Main {    public static void main(String[] args) {        FeatureMap featureMap = new FoodFeatureMap(Chinese);        Map<String, Object> map = Collections.singletonMap(FoodFeatureMap.foodType, Chinese);        //assume this map contains more features fetched from somewhere given the earlier map having keys        featureMap.createFeatureMapWithMandatoryValues(map);    }}
Need a better way to create subclass instances from a base class reference
java;design patterns