Co-variant data types are associated with using the generic type to refer to the concrete type. Sample source code showing the use of Map as co-variant return type follows:

package com.example;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class Foo{

	public Map getMap() {
		System.out.println("Foo");
		return new HashMap();
	}

	public static void main(String[] args) {
		Foo f = new Bar();
		f.getMap();
	}
}

class Bar extends Foo{
	public HashMap getMap() {
		System.out.println("Bar");
		return new LinkedHashMap();
	}

}

Output:
Bar

In the above code, both version of getMap method specify return type as Map and return instances of HashMap and LinkedHashMap.

One important point to note about co-variant data types in Java is that two methods with same name, arguments can’t be differentiated based on return types. There was a bug related to introspection of bean which caused issues with IoC containers. This bug has been fixed in JDK 1.7. Do read this interesting story on bean-introspection.

Co-variant return types is not a big concept and almost every intermediate developer in Java knows about it. But still if you have understanding the concept then do leave a comment regarding the same.

What are Co-variant return types in Java admin Core Java
Co-variant data types are associated with using the generic type to refer to the concrete type. Sample source code showing the use of Map as co-variant return type follows: package com.example; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class Foo{ public Map getMap() { System.out.println("Foo"); return new HashMap(); } public static void main(String Output: Bar In the above code, both version of...
Co-variant data types are associated with using the generic type to refer to the concrete type. Sample source code showing the use of Map as co-variant return type follows: 1 Output: Bar In the above code, both version of getMap method specify return type as Map and return instances of <a href="http://www.javaexperience.com/differences-between-hashmap-and-hashtable-in-java/" title="Differences between HashMap and Hashtable in Java">HashMap and LinkedHashMap</a>. One important point to note about co-variant data types in Java is that two methods with same name, arguments can't be differentiated based on return types. There was a bug related to introspection of bean which caused issues with IoC containers. This bug has been fixed in JDK 1.7. Do read this interesting story on <a href="http://java.dzone.com/articles/covariant-return-type-abyssal" rel="nofollow">bean-introspection</a>. Co-variant return types is not a big concept and almost every intermediate developer in Java knows about it. But still if you have understanding the concept then do leave a comment regarding the same.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.