3 ms·
It's not quite fair to say Java lacks polymorphism, it just doesn't extend to primitive types.
by neilk 13y ago
It's not quite fair to say Java lacks polymorphism, it just doesn't extend to primitive types.
- kyllo 13y agoWell, apparently "polymorphism" in the Java world just means that subclasses can override the superclass's methods. http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html http://docs.oracle.com/javase/tutorial/java/IandI/polymorphi... Yes, the Java 1.5+ generic data structure classes that take parameterized types allow a sort of polymorphism, but only for objects (including wrapper classes), but it's not quite as powerful as functional polymorphism. For example, in SML you can swap an ordered pair of any two datatypes with one line of code: fun swap(x: 'a, y: 'b): 'b * 'a = (y,x) And to illustrate my other point about type inference, this is also legal SML and does the same thing. You can leave the type annotations off and the compiler will figure them out for you: fun swap(x,y)= (y,x) where x and y can each be any datatype. I don't really know Scala but from reading about it I get the impression that it also allows this. Have fun doing it in Java though... you'll have to implement your own generic ordered pair class.