Java Generics Covariant and Contravariant

Introduction

Here the getter method

( ) => A

Covariant

A <: B

( ) => A <: ( ) => B

If a A is subtype of B, then getter or A is subtype of getter of B.

Here the setter method

A => ( )

Contravariant

B => ( ) <: A => ( )

Above Covariant and Contravariant can be explained from the following code:

Say A is Animal

public class Animal {
@Override
public String toString(){
return "I am a Animal";
}
}

Say B is Bear as follows

public class Bear extends Animal {
@Override
public String toString(){
return "I am a Bear";
}
}
 

As shown in the above code Bear is subtype of Animal.

Here the Zookeeper class where getter and setter methods are

public class Zookeeper {
private Animal animal;
public void setFeed(Animal animal){
this.animal =animal;
}

public Animal getFed(){
return this.animal;
}
}

As shown in the above Zookeeper class, there is no indication of Bear, but in the following Zoo client code

public class Zoo {
public static void main(String ...args){

Zookeeper zookeeper = new Zookeeper();
Bear bear = new Bear();
zookeeper.setFeed(bear);
Bear retCat = (Bear) zookeeper.getFed();
System.out.println(retCat);

}
}

In the method setFeed you can pass the Bear instance according to the contravariant subtyping, and in the getter method getFed returns the Bear instance as a reference of Animal that is because getters support covariant subtyping.

Invariant

But how to explain this concept. See the Bearkeeper class which is subtype of Zookeeper

/**
* Created by Ojitha on 9/11/2014.
*/
public class Zookeeper {
private Animal animal;
public void setFeed(Animal animal){
this.animal =animal;
}

public Animal getFed(){
return this.animal;
}
}

class BearKeeper extends Zookeeper{
Bear bear;

@Override
public void setFeed(Animal animal) {
this.bear = (Bear)animal;
}

public void setFeed(Bear bear){
this.bear = bear;
}

@Override
public Animal getFed() {
return this.bear;
}
}

As show in the above code, void setFeed(Animal animal)  override the method in the Zookeeper because subclass return the covariant of super return , while overloading with the method void setFeed(Bear bear) method because of invariants of setter methods in Java although Bear is subclass of Animal.

Arrays

Java Arrays support covariant but this is not a good idea. This made Java to throws the bad runtime exception java.lang.ArrayStoreException in the following code:

        Animal[] animals = new Animal[]{new Animal(), new Animal(), new Bear()};
Bear[] bears = new Bear[]{new Bear(), new Bear()};
animals = bears;
animals[1] = new Animal();

As shown in the above code, last line java.lang.ArrayStoreException has thrown to indicate that Animal instance is not a valid instance anymore because animals reference became a Bear type after the animals = bears assignment.

Array doesn’t support contravariant subtyping Sad smile.

Generics

In term of generic, array subtyping covariant means that type S[] is considered to be subtype of T[] wherever S is subtype of T.  Other hand, the subtyping relation for generics is invariant: List<S> is not considered to be subtype of List<T>. Therefore, wildcards present the covariant subtyping for the generics: List<S> is subtype of the List<? extends T> where S <: T. Wildcards introduce contravariant subtyping as well. For example, List<T> is subtype of List<? super S>.

Here the example for congtravariant

        List<Object> objs = new ArrayList<Object>();
objs.add(1);
objs.add(1.23);
List<? super Integer> integers = objs;
Object o = integers.get(1);
//Integer i = integers.get(0);

As shown in the above,  Object is a supper class of the Integer. Even Integer as well as Double has been added. The values are read as Object only. It is impossible to read as Integers. The is where GET/PUT principal comes to the play, that says “use extend if you are reading from the structure, as well as use super if you writing to the structure.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python