1

If the method in the parent class does not need a parameter, but the overriding method in child class needs one. Is it good practice to have a no use argument in the method in parent class?

//in parent class:
getFeature(String name){
    return defaultFeature;
}

//in child class:
getFeature(String name){
    return new Feature(name);
}

This method is used and only used in another method in parent class. So we have the same signature for parent and child classes. The real problem has two child classes with a little different implementation of getFeature.

Or is there a better way to accomplish this?

5
  • 4
    If you do that, it's not overriding anymore, it's overloading. Commented Feb 8, 2018 at 15:59
  • @LuiggiMendoza: Not really, vis-a-vis the last sentence of the question. Commented Feb 8, 2018 at 16:02
  • This looks to me as the abstract factory pattern, except that the parent class isn't abstract and returns some default object. Anyway, methods with different signatures (name + parameter list) are different methods, so you cannot have a method getName() in the parent class, and a method getName(String name) in the child class, and hope to override it. They don't have the same signature. Commented Feb 8, 2018 at 16:04
  • But this getFeature is used and only used in another method in the parent class. That is where the polymorphism happens. Commented Feb 8, 2018 at 16:07
  • From your last comment, this seems like a possible code smell. Could you explain further about the design of the parent class? Is this name parameter always needed? Commented Feb 8, 2018 at 16:16

2 Answers 2

2

Is it good practice to have a no use argument in the method in parent class?

It depends. If the API design anticipates that subclasses will need the parameter even though the base class doesn't, and you want to ensure that the parameter version of the method can be called through a parent-typed reference:

Parent instance = new Subclass();
Feature f = instance.getFeature("foo");

...then you have to include the parameter in the parent class signature even though you don't use it in the implementation. You might also have the no-parameter version which always provides the non-parameter functionality:

class Parent {
    public Feature getFeature() { // Maybe you'd want this, maybe not
        return defaultFeature;
    }
    public Feature getFeature(String name) {
        return this.getFeature();
    }
}
class Subclass extends Parent {
    @Override
    public Feature getFeature(String name) {
        return /*...*/;
    }
}

But if you anticipate that subclass instances will only be used with subclass-typed references:

Subclass instance = new Subclass();
// ^^---------- note
Feature f = instance.getFeature("foo");

...then there's no reason for the parent class's getFeature to have it. Subclasses can add it as an overload:

class Parent {
    public Feature getFeature() {
        return defaultFeature;
    }
}
class Subclass extends Parent {
    public Feature getFeature(String name) {
        return /*...*/;
    }
}

It just means that Parent-typed references will be limited to the no-parameters feature of getFeature.

Sign up to request clarification or add additional context in comments.

1 Comment

In the first approach, the problem (or downside) is that the parameterless method will still be accessible in the subclass object. There are always some trade-off we need to make in these situations.
2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.