Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited method needs one more parameter

I have a parent class with several children. One of the children, has one overridden method that, for its particular internal usage, needs one more parameter. I don't want to change the method's signature because the overridden method in the other children does not need this parameter, also, I don't want to add a class property because it is pointless in this case.

How do you deal with these situations?

So far I've added a NotImplementedException in the method and created a new one, but it really is something I've done while I wait for an answer, I don't want to do it this way.


Edit after Jon Skeet's answer

I'll try to figure out if I've understood what Jon suggested. This is quite interesting.

public abstract class Parent {
    public abstract void aMethod(Object parameter);
}

public class NotReallyParentChild {
    public Parent createInstance(){
        return new Child();
    }
}

public abstract class Child extends Parent {

}

Mmmmh no I'm completely wrong here, I don't understand the second part of your post, could you please shed some light on this?

like image 216
Alberto Zaccagni Avatar asked Oct 19 '09 09:10

Alberto Zaccagni


People also ask

Can you override a method with different parameters?

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.

Can we override methods in inherited classes?

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

Can we override with inheritance?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.

How can we avoid inheritance of methods and variables?

Using final keyword to prevent method overriding during inheritance: All methods and variables can be overridden by default in a subclasses. If we need to prevent the subclasses from overriding the members of the superclass, we can declare them final .


2 Answers

It sounds like you don't really have a proper inheritance relationship there - it breaks Liskov's Subtitutability Principle, because you can't use this "child" as if it were an instance of the parent, without extra knowledge.

How about making the "child" class not derive from the "parent", but provide a method on the child which will create an instance of some subclass of the parent, which "knows" the appropriate extra bit of information? If you could give us more information about what the classes are doing, we could give more concrete examples of what the code might look like.

EDIT: It's really hard to try to make this much clearer without knowing more about what you're doing, but something like:

public class NotReallyParentChild {
    public Parent createInstance(Object extraParameter){
        return new ChildWithParameter(extraParameter);
    }
}

That ChildWithParameter class could then override the appropriate method without taking any extra information, because it already had it as part of its own internal state.

like image 172
Jon Skeet Avatar answered Sep 20 '22 02:09

Jon Skeet


Whilst agreeing with Jon's view that the inheritance model is broken, you can handle scenarios like this by using special parameter objects. That is, instead of having a method:

void myMethod(Object param1, Object param2, Object param3...);

you have an object ParameterObject which contains the parameters required, and your method looks like:

void myMethod(ParameterObject param);

That way, you can add parameters to the parameter object, and not affect or change the interface and implemented methods.

If I'm writing a method and the number of parameters seems excessive, and I believe my encapsulation is not broken in any way (this happens a lot in financial software when equations/models take a lot of inputs), then I find the above a very useful pattern.

like image 28
Brian Agnew Avatar answered Sep 19 '22 02:09

Brian Agnew