Generic java configuration class with type safety
Generics can only go so far to help you, and, this is one of those times where it just does not help all that much.

In essence, you have the following:


a configuration system where you can get values based on names (a Map).
when you set a value it is for a given type (which is either specified when you set the value, or it is not, in which case it is inferred).
when you get a value, it is matched against the type the getter expects, and, if it matches, it is returned.


Since the getter/setter is the only place where you actually know the type of the value, there is actually zero benefit for your entire Setting subclass... it is redundant. You can get the same behaviour with:

private final Map&lt;String, Object&gt; options;

public DefaultConfiguration() {
    this.options = new HashMap&lt;String, Object&gt;();
}

@Override
public &lt;T&gt; T get(String option, Class&lt;T&gt; type) {
    Object value = options.get(option);
    if (value != null) {
        return type.cast(value);
    }
    return null;
}

@Override
public &lt;T&gt; void set(String option, T setting, Class&lt;T&gt; type) {
    // no point in even checking the type, the generic method will enforce that.
    // this method is essentially redundant, and could be
    //   public void set(String option, Object value);
    options.put(option, setting);
}

@Override
public void set(String option, Object setting) {
    // this essentially a duplicate option.
    options.put(option, setting);
}


here you have the exact same functionality as you did before.... if the user wants to, they can compile-time type-check their values when they set them (by ensusuring the Class&lt;T&gt; parameter matches the T setting value

When they retrieve the value, they can cast the result to the specified type, or return null if it is not castable.

The functionality in MyConfiguration is unchanged, but, I recommend you use constants for the keys:

private static final String SETTINGA = "settingA";
....


Finally, you may want to consider your DefaultConfiguration extending something like HashMap directly.... like:

public class DefaultConfiguration
          extends HashMap&lt;String,Object&gt;
          implements Configuration {


then, remove the options Map variable, and simply have methods like (note the direct get(option) instead of options.get(option):

@Override
public &lt;T&gt; T get(String option, Class&lt;T&gt; type) {
    Setting setting = get(option);
    if (setting != null) {
        return type.cast(setting.getSetting(type));
    }
    return null;
}


Now, when people use your class, they have access directly to the configuration too (with, and without your 'helper' methods).

The bottom line though, is that your implementation does not enforce type-safety between the set and the get. The only thing it does is return null instead of throwing a ClassCastException when people use your code wrong.