Java lesson on generics are leading me to variance concept. This causes me some headaches as I cannot find a very simple demonstration of what it is.
I have read several similar questions on stackoverflow, but I found them too difficult to understand for a Java learner. Actually the problem is that the explanation of generics requires variance to be understood, and the concept of variance is demonstrated relying heavily on generics understanding.
I had some hope reading this, but in the end I shared C. R.'s feeling:
The title reminds me of the days learning general relativity. – C.R. Dec 22 '13 at 7:34
Four theory questions are very confusing to me, and I cannot find good and simple explanations. Here they are, with my current partial understanding (I fear experts will have a great fun reading this).
Your help to correct and clarify is welcome (remember this is for beginners, not for experts).
Is there something wrong with this understanding?
- What is invariance / covariance / contravariance related to in the context of programing? My best guess is that:
- This is something encountered in object-oriented programing.
- This has to do when looking at method arguments and result type in the class and an ancestor.
- This is used in the context of method overriding and overloading.
- This is used to establish a connection between the type of a method argument, or the method return type, and the inheritance of the classes themselves, e.g. if class D is a descendant of class A, what can we say about the types of arguments and the method method return type?
- How variance relates to Java methods? My best guess is that, given two classes A and D, with A being an ancestor of D, and a overhiden/overloaded method f(arg):
- If the relation between the argument type in the two methods IS THE SAME than the relation between the two classes, the argument type in the method is said COVARIANT with the class type, said otherwise: the inheritance between arg types in A and D is covariant with the inheritance of classes A and D.
- If the relation between the arguments REVERSES the relation between classes, the arg type is said CONTRAVARIANT to the class type, said otherwise: the inheritance between arg types in A and D is contravariant with the inheritance of classes A and D..
- Why is variance understanding so important for Java programmers? My guess is that:
- Java language creators have implemented rules for variance in the language, and this has implications on what a programmer can do.
- A rule states that the return type of an overriding/overloading method must be contravariant to the inheritance.
- Another rule states that the type of an argument of an overriding/overloading must be is covariant to the inheritance.
- The Java compiler checks the variance rules are valid, and provides errors or warnings accordingly. Deciphering the messages is easier with variance knowledge.
- What is the difference between overrhiding and overloading? Best guess:
- A method overrides another method when argument and return types are both invariant. All other cases are understood by the compiler as overloading.