Actual you override and it is a misleading approach as the client of the classes will have to pass a String while this is used only in the child class :
child.getFeature("makeSenseArg");
parent.getFeature("notUsedArg");
You should overload the getFeature() method in the child class:
//in parent class:
getFeature(){
return defaultFeature;
}
//in child class:
getFeature(String name){
return new Feature(name);
}
You would not benefit any longer of the polymophism but it should probably not be an issue as these are two distinct behaviors.
Merging them in a single method signature is maybe not the best thing to do.
Note that if you really need to rely on overriding, you could annotate the parameter with @javax.annotation.Nullable.
It makes the API clearer for the class clients and in addition code analysis tools such as Sonar takes also these annotations into consideration.
This would give :
// parent class
Feature getFeature(@Nullable String name){
return defaultFeature;
}
Besides, if for the overrided method, the parameter is mandatory, you could even specify the "reverse" annotation (@javax.validation.constraints.NotNulll) :
//child class
Feature getFeature(@NotNulll String name){
return new Feature(name);
}
Clients would call them :
child.getFeature("makeSenseArg");
parent.getFeature(null);
method getName()in the parent class, and a methodgetName(String name)in the child class, and hope to override it. They don't have the same signature.nameparameter always needed?