Skip to content
Advertisement

Define Methods in Interface Without Specifying Number of Parameters

I’m trying to implement classes from the same interface but with a different number of parameters in method like the code below.

JavaScript

Is there a way in Java to define methods in the interface without constraining the number of parameters?

Advertisement

Answer

You could use the ellipsis syntax (...) and then check the number of arguments passed to the method in runtime:

JavaScript

But this completely misses the point of having an interface and implementing its methods.

The idiomatic way to handle this in Java would be to have each shape take the appropriate arguments in its constructor and implement a no-argument getArea() method that relies on its data members:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement