Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloaded methods in interface

my question for today: are overloaded methods in interface bad? You know, the "omit parameters if you don't care, we'll figure out the default values" kind of overloaded methods. Like that:

void Add(object item); 
void Add(object item, bool shouldDoSomething); 
void Add(object item, bool shouldDoSomething, IUltraObscureDeviceContext context);

In this case I tend to think that only the latter belongs to an interface and others should be implemented in abstract class on top of it. But then again, I'm not sure.

Also, there are times when you just want different overloads doing slightly different work (stop me right there if overloaded methods should never be used for that). Or sometimes you can't just stuff nulls in place of some parameter, you want the exception thrown if something is null. Should I not use overloading in this case?

So basically I'm looking for some guidelines on overloaded methods in interfaces vs overloaded methods in abstract classes implementing these interfaces and so on. Thanks in advance

like image 267
Dyppl Avatar asked Jan 26 '11 05:01

Dyppl


People also ask

CAN interface have overloaded methods?

Yes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.

Can interface methods be overloaded in Java?

The correct answer is option4. Key Points Both functions and operators can be overloaded. C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. Hence the correct answer is function.

Which methods are overloaded?

Overloading happens when you have two methods with the same name but different signatures (or arguments). In a class we can implement two or more methods with the same name. Overloaded methods are differentiated based on the number and type of parameter passed as arguments to the methods.

Can we overload default methods in interface in Java?

If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface. In short, you can access the default methods of an interface using the objects of the implementing classes.


2 Answers

If the defaults depend on the receiver of the method call, declare them as interface methods.

If the defaults are just defaults irrespective of the receiver, create the various reduced-argument-list overloads as extension methods and save implementors the headache of having to supply all the overloads themselves.

If you're dealing with some sort of nitty-gritty 80/20 rule exceptions, where implementation-independent defaults are almost but not quite always sufficient, you have a few options, none of which are that good:

  • Deal with it as if they're always different, declare them as interface methods, and reimplement them everywhere. Not very DRY.
  • Deal with it as if they're always different, declare them as interface methods, and inherit a base class that provides the 80% default implementation. Kind of clumsy, and not good if your sole base-class slot is already occupied.
  • Create another interface containing those specific methods. Declare extension methods with matching signature against the original interface. In the extension methods, as-cast the this argument to the new interface type, and if it matches, call it there, otherwise fill in stock defaults. Very funky, reliant on dynamic casting so not that great in an inner loop, but it decouples the defaults from both implementor and caller without sacrificing flexibility for implementors that can't take the "default defaults".
like image 152
Jeffrey Hantin Avatar answered Sep 26 '22 01:09

Jeffrey Hantin


Overloaded methods in an interface maybe Ok if the design of the interface warrants it. I've personally never needed to do this.

I wouldn't define default parameters values in an interface. That is an implementation detail that implementing classes that choose to surface.

you comment about having different implementations for the overloads....

Don't do that. Think of implementation of these overloaded methods as calling the one method that has all of the parameters defined with some default values. That's how users of your code would expect things to be.

If you need different behavior then use polymorphism. That's what its there for.

like image 22
Shiv Kumar Avatar answered Sep 22 '22 01:09

Shiv Kumar