Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
What Java version? Java 8 has the possibility for code such as Arrays::<@NonNegative Integer>sort with things such as code.google.com/p/jsr-305/source/browse/trunk/ri/src/main/java/… –  MichaelT Apr 4 at 19:19
    
@MichaelT: The code that you cite is part of sample code for a JSR that is still very early in the implementation stages (creation of the expert group). It does not seem to have been implemented as part of Java 8. –  jhominal Apr 4 at 19:27
    
@jhominal Being an JEE guy, I'm stuck way behind the curve and just glance at the new things I'll get in a few years... types.cs.washington.edu/jsr308/specification/… seems to be more appropriate that was mentioned in mscharhag.com/2014/02/java-8-type-annotations.html –  MichaelT Apr 4 at 19:32
add comment

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
There is, of course, validation. Granted, you have to implement that in the class, not the interface. –  Robert Harvey Apr 4 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. –  Doval Apr 4 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? –  user2738698 Apr 4 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. –  Doval Apr 4 at 19:37
1  
"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. –  Jörg W Mittag Apr 5 at 0:32
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.