| Covariance And Contravariance (computer Science) |
Article Index for Covariance |
Information About ™Covariance And Contravariance (computer Science) |
This is contrasted with ''invariance'', where arguments have to be of exactly the same type. As is usual in the case of object oriented programming, type can be a Class and supertype can be any Superclass or any implemented Protocol Or Interface . Covariant parameters are not safe, but covariant return types and exceptions are. Similarly, contravariant parameters are safe, but contravariant return types and exceptions are not. This is because the caller may expect the original type, but in subtyping it doesn't matter if the overriding method ''cares less'' about parameters and ''cares more'' about return values and exceptions than expected. The issue is somewhat implicit in object-oriented design and programming in general, but becomes explicit in Generic Programming , for example. LANGUAGE SUPPORT Eiffel In Eiffel you can get Compiler errors, because Polymorphism breaks. Java Return type and exception covariance is implemented in the Java programming language version J2SE 5.0. Parameter types have to be exactly the same (invariant) for method overriding, otherwise the method is overloaded with a parallel definition instead. REALbasic REALbasic added support for return type covariance in version 5.5. As with Java, the parameter types of the overriding method must be the same. Scala Scala supports both covariance and contravariance. SEE ALSO
EXTERNAL LINKS
|
|
|