Java covariance, contravariance [Goto]

Source: Internet
Author: User

n covariance, contravariance and independence

In object-oriented computer programming languages, conversions between types are often involved, such as type conversions from specific cat types to animals (upstream conversions), or transitions from shapes to triangles (downstream conversions).

Covariance (covariance), inverse degeneration (contravariance) , and Independence (invariant). They are all terms used to describe the nature of the type conversion, and the formal description is as follows:

If a and B are types,f represents a type conversion, and ≤ represents a subtype relationship (for example a≤b, which means a is b Sub-Class) Then:

If a≤B is f (a) ≤ f (B) then F is covariant

If a≤B is f (b) ≤ f (A) then F is the inverse

If neither of the above is true, then F is a irrelevant of the

n Java language has some syntax that can be understood by covariance and contravariance

u Array

There is a loophole in the covariance group, see: static type vulnerability caused by array covariance rednaxelafx.iteye.com/blog/379703

Attention:
For example a =string, B = Object We have a≤b here
F (A) = string[], f (B) = object[].
because object[] objects =new string[n]. (Assignment) So we can assume that the array has covariance .

!--[if!supportlists]-->u covariance return value (when subclasses override method)

overriding at the time of this method , the return value of the method can be overridden by a "narrower" type.
(c# does not support this technology, c++ and java JDK5.0 after support)
For example:

 public  static  class   Super {Object getsomething () { return  null  ; }}  public  static  class  Subclass  Super {@Override String getsomething () { return  null
    

The main idea of this passage is that the synergistic return value is used when the subclass needs a return value that differs from the parent class override method, but the return value has an inheritance relationship .
An example that might be better is as follows:

class Collection {      Iterator Iterator () {...}}   class extends Collection {     @Override      listiterator iterator () {...}}  

Iterator The function obtains an iterator to the current collection, and in the subclass the iterator has a more precise representation, so the subclass Listiterator of Iterator is used as the new return value.

!--[if!supportlists]-->u contravariant parameter parameter
First, for example, you can see an example of a contravariant parameter, In fact, the inverse parameter using the environment is still under the inheritance system function overrides .

 class   Super { void   DoSomething (String parameter) {}}  class  Sub extends   Super { void  dosomething (Object parameter) {//  Invert   

liskov Substitution Principle the concept of . The content of this principle is that an object of the subclass can be used wherever the parent object is needed .
What is the relationship between this principle and the parameter and the return value?
If we define the class as follows:

 class   Super {returntype dosomething (Param Etertype a) { return  null  
   
    ; }}  
    class  Sub 
    extends  
     Super {ReturnType dosomething (ParameterType a) { 
    return null  
   

If I had Super sup;
First consider the parameters and now call sup.dosomething (o);
If the two functions require the same type of parameters, there is nothing worth discussing. If, in some cases, asub requires a parameter type that becomes another class Newparametertypethat has an inheritance relationship with parametertype . As follows:

class extends Super {           returntype dosomething (Newparametertype a) {                 returnnull;           

if Newparametertype is a subclass of ParameterType , then the sup.dosomething (o) call, if o is a pointer to the Newparametertype object, it will not be passed. Only if Newparametertype is the parent of parametertype , no problem will occur .(The formal parameter must be changed to be contravariant only ). So the concept of contravariant parameters is proposed.
Similarly, the use of this principle can also be used to understand.
If you have the following statement in a usage environment:
ReturnType RT = Sup.dosomething (o);
If the design changes, the subclass needs to return a class Newreturntypethat has an inheritance relationship with returntype , if the parent class of ReturnType is returned, Then the above statement can not be compiled, only Newreturntype is a subclass of ReturnType , will not violate the Liskov sub-class principle.

!--[if!supportlists]-->ujava generic
in java generics, for example

class Dataholder<t>{}

If type A ≤ B, but the direct use of dataholder<a> and dataholder<b> is not covariant , We've already described it before.

But with the wildcard syntax provided by JAva , it is possible to provide a covariant type conversion .
dataholder<a>
≤dataholder<? Extends b>//? is a wildcard, but this covariance is type insecure

For example:

Static Boolean extends Object> where, Object what) {//wildcard? Assign to a generic variable, you can have a covariant type convert         returnfalse

Covariant type conversions: You can use iterable<string> to invoke the find function.

Java covariance, contravariance [Goto]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.