Demonstrate covariance and contravariance in java?

Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because it takes a parameter of a superclass of the parameter of Super#doSomething (but, again, fullfills the contract of Super#doSomething) Generics This is also possible for Generics: List aList... List covariantList = aList; List contravariantList = aList You can now access all methods of covariantList that doesn't take a generic parameter (as it must be something "extends Object"), but getters will work fine (as the returned object will always be of type "Object") The opposite is true for contravariantList : You can access all methods with generic parameters (you know it must be a superclass of "String", so you can always pass one), but no getters (The returned type may be of any other supertype of String).

Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because it takes a parameter of a superclass of the parameter of Super#doSomething (but, again, fullfills the contract of Super#doSomething) Generics This is also possible for Generics: List aList... List covariantList = aList; List contravariantList = aList; You can now access all methods of covariantList that doesn't take a generic parameter (as it must be something "extends Object"), but getters will work fine (as the returned object will always be of type "Object") The opposite is true for contravariantList: You can access all methods with generic parameters (you know it must be a superclass of "String", so you can always pass one), but no getters (The returned type may be of any other supertype of String).

The first example of contravariance doesn't work in Java. DoSomething() in the Sub class is an overload, not an override. – Craig P.

Motlin Apr 14 at 22:26.

Co-variance: Iterable and Iterator. It almost always makes sense to define a co-variant Iterable or Iterator. Iterator can be used just as Iterator - the only place where the type parameter appears is the return type from the next method, so it can be safely up-cast to T.

But if you have S extends T, you can also assign Iterator to a variable of type Iterator. For example if you are defining a find method: boolean find(Iterable where, Object what) you won't be able to call it with List and 5, so it's better defined as boolean find(Iterable where, Object what) Contra-variance: Comparator. It almost always makes sense to use Comparator, because it can be used just as Comparator.

The type parameter appears only as the compare method parameter type, so T can be safely passed to it. For example if you have a DateComparator extends Comparator { ... } and you want to sort a List with that comparator (java.sql. Date is a sub-class of java.util.

Date), you can do with: void sort(List what, Comparator how) but not with void sort(List what, Comparator how).

You find some info about it on wikipedia: en.wikipedia.org/wiki/Covariance_and_con...)#Java.

Thx .. But I cant get the detailed information . Why covariance/contravariance came into picture? – JavaUser Mar 23 '10 at 15:19.

Look at the Liskov substitution principle. In effect, if class B extends class A then you should be able to use a B whenever an A is required.

Better point out ..cool! – JavaUser Mar 23 '10 at 15:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions