0

As far as I know, declaring an interface is like so:

public interface Roots_Squares {
 public double square_root( double value );
}

Now... how do you enforce value to have non-negative values? Such a function should avoid negative values right? If someone creates a class that implements this, and that implementation does not handle negative values, and then I use their class with a negative value... well, things break.

It then becomes my responsibility to check for negatives when I'm pretty sure the implementor should do the checking.

asked Apr 4, 2014 at 19:11
4

4 Answers 4

3

As explained a bit more thoroughly in this answer, that's (part of the) price you pay for an interface. If you want to be able to use new implementations of your abstraction at any time, you have no way of forcing those implementations to be correct.

answered Apr 4, 2014 at 19:18
10
  • There is, of course, validation. Granted, you have to implement that in the class, not the interface. Commented Apr 4, 2014 at 19:20
  • @RobertHarvey But someone always has the option of not using your class and producing a totally new and bogus instance of the interface. Commented Apr 4, 2014 at 19:22
  • So how about, instead of double, a custom, non-negative derived class of doubles? So the function becomes "public double square_root( Positive_Double value );" or would that be too much of stretch? Commented Apr 4, 2014 at 19:29
  • @user2738698 There's a couple of issues. 1) I wouldn't make it a subclass of the double class because it doesn't behave like a double. Particularly, doubles are closed under subtraction - subtracting two doubles results in another double. For positive doubles though, 3.0 - 5.0 blows up because -2.0 is not a positive value. Commented Apr 4, 2014 at 19:37
  • 2
    "There's no mechanical way to ensure a program won't generate a negative number during the course of its execution without actually executing it." – Only if the language is Turing-complete. In dependently-typed languages, types such as "non-negative integer" or "list of length n, where n is a runtime value" can be expressed, and can be statically type-checked. Commented Apr 5, 2014 at 0:32
0

The comments on Doval's answer are right: create and use a PositiveDouble. Also, consider using a design by contract tool. A design by contract tool like cofoja might solve your problem. I have not used 1 but I know that there are several design by contract tools in Java.

answered Nov 26, 2014 at 20:49
1
  • The other comments are right, however, in that there is no realistic way of creating such a type in a traditional object-oriented language like Java. It requires much better type inference than is available in such languages in order to not be a nightmare to use such a type. Commented Nov 27, 2014 at 7:58
0

Your interface should document how it handles bogus input, and can declare Exceptions it will throw. In this case, a simple

@throws IllegalArgumentException would do the trick.

answered Nov 27, 2014 at 0:46
-1

Doval's answer is outdated now. You can use an enforce parameter properties(check arguments) in the interface:

public interface Math {
 /**
 * @throws IllegalArgumentException if value <= 0
 */
 default public void sqrt(Double value) {
 if(value <= 0) throw new IllegalArgumentException("value must be greater than 0");
 squareRoot(value);
 }
 public void squareRoot(Double value);
}
answered Nov 30, 2014 at 7:46
1
  • Programmers is about conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler Commented Dec 2, 2014 at 21:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.