Bounded wildcard

I have been interviewing candidates for the position of software architect. Invariably I find that the candidates’ knowledge is at a cursory level. This is surprising because the person has atleast 10 years’ experience or more. The offshore environment has muddled these people because they are never allowed to own the entire software stack including the deployment architecture. There is always a foreign architect who guides them and ensures that they don’t have wide knowledge of architectural concerns.

They also don’t have to be aware of the latest Java developments. So a question about generics which was introduced several years back elicits no response.

I am interested in learning about generics but sometimes I am befuddled by the complexity. ‘Effective Java’ by Bloch has some good advice. One of it is shown below.


public interface Parent extends Comparable< Parent> {
}

public interface Child< T> extends Parent{

}

public class Compare {

//Incorrect Test method
// public static < T extends Comparable< T>> T compare(List< T> list){
//
// return list.get(0);
// }

//Correct Test method
public static < T extends Comparable< ? super T>> T compare( List< ? extends T> list){

   return list.get(0);
}

public static void main( String... s ){

    List< Child< ?>> children = new ArrayList< Child< ?>>();
    compare( children );

}
}

The correct way to use bounded wildcards to compile this method is shown in the book. If one uses the incorrect method it does not compile. The reason is that Child instances are not only comparable to themselves but also to Parent instances.

Generics is one subject that makes me think that even after reading a very good explanation I am still at a loss. But I want to capture patterns like this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: