简体   繁体   中英

When we override a method we should use all the parameters that are in the method signature?

I found some overrides methods that not use all the parameters that are in the signature of the method.

ex:

@Override
protected void setSomething(Object a, Object b, Object c) {
    this.a = a
    this.b = b;
    // the parameter c is not used (ignored)
}

Normally the parent class shouldn't be care about how the children will implements the abstract methods.

But in MHO, arguments of a method are to be use, it's very rare when a sub-class implementation doesn't need a parameter, when this happens then probably there's a problem with the design of the interface or with the abstract class.

The base of one function is: inputs -> process of inputs -> output.

Sometimes you need to calculate these inputs, but if you don't go to use some of these inputs in the process of your function, these inputs shouldn't be place as inputs of your function.

You could jump the calculation of these inputs calling an accurate function that use all the inputs, so the accurate function.

The only case where this situation could be acceptable, is when we don't want the behavior of the parent class, so we could write:

@Override
protected void setSomething(Object a, Object b, Object c) {
      //Nothing to do
}

Or

@Override
 protected void setSomething(Object a, Object b, Object c) {
  throw new UnsupportedOperationException(...);
}

Sonar says :

Unused parameters are misleading. Whatever the value passed to such parameters is, the behavior will be the same.

My Question is: When we override a method we should use all the parameters that are in the method signature?

When I says 'use all the parameters', I try to say that all the parameters that are in the method signature, are really use in the body (implementation) of the method.

When we override a method we should use all the parameters that are in the signature of the method?

When you override a method, the overriden method must define the same parameters as the super-method.

You're not obligated to use all the parameters in the implementation - this depends on what you want to achieve with this implementation and sometimes not all parameters may be needed.

However, having unused method parameters within the method implementation is a sign of a poor design. When defining a method (being abstract or implemented), you should try answer the questions of "Why would I need this parameter?" and "Will this parameter be always used?" . If it's possible to have cases where some parameters will not be used in the implementation, then you could define a few overloaded methods.

Take this example, for instance. Let's have this method

void someMethod(String first, String optionalParameter) { ... }

The second parameter is optional (ie may be needed or may not be) - you could pass null or anything when the parameter is not needed. In this case, I would overload two methods

void someMethod(String first) { ... }

void someMethod(String first, String second) { ... }

and I will also make sure that all the parameters are used in the corresponding implementation.

Do you need to use all the parameters? No. You will often see examples like:

@Override
public void doFoo(String thingy) {
  // no-op
}

Or

@Override
public void doFoo(String thingy) {
  throw new UnsupportedOperationException(...);
}

But both are a sign of questionable design, somewhere. java.util.List or even java.util.Iterable , for example, both preclude the possibility of an immutable collection, by providing mutation methods. Immutable implementations have to throw UnsupportedOperationException .

If you override a method, than yes, you must use all the parameters.

But you can also overload a method - write a method with same name but different parameters. So if you dont use all of the parameters, you are overloading, not overriding.

class Accessor {

    public void doSomething(String attr) {}

}

class Child extends Accessor {

    // This is overriding
    @Override
    public void doSomething(String attr) {
       // ...
    }

    // This is overloading
    public void doSomething() {
       // ...
    }
}

As you said: "we should" - but we don't have to. Sometimes an implemnting method even throws a RuntimeException , eg UnsupportedOperationException of the Java Collections Framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM